Writing authentication behaviour

76 views
Skip to first unread message

gleb Chermennov

unread,
Aug 2, 2012, 4:05:34 PM8/2/12
to fubumv...@googlegroups.com
I'm new to the framework and I'm trying to make custom authentication logic work in my app.
Here's the scenario - if a user hits a url and he hasn't been authenticated yet, he's being redirected to the login screen. Nothing fancy here. But after he successfully authenticated, I want to redirect him to the page he intended to visit originally. 
e.g. user tries to reach /posts/edit/1, he's redirected to the login screen. after he's logged in, I want the app to automagically redirect him to /posts/edit/1. 
I get the part about redirecting to login screen working thanks to Rex Morgan's post. Now, how can I redirect user to the original url?
Here's my behaviour for doing this:
    public class AuthenticationRequiredBehaviour: BasicBehavior
    {
        private readonly ISecurityContext securityContext;
        private readonly IUrlRegistry urlRegistry;
        private readonly IOutputWriter outputWriter;

        public AuthenticationRequiredBehaviour(ISecurityContext securityContext, IUrlRegistry urlRegistry, IOutputWriter outputWriter): 
            base(PartialBehavior.Ignored)
        {
            this.securityContext = securityContext;
            this.urlRegistry = urlRegistry;
            this.outputWriter = outputWriter;
        }

        protected override DoNext performInvoke()
        {
            if (securityContext.IsAuthenticated())
            {
                return DoNext.Continue;
            }
            var url = urlRegistry.UrlFor<LoginOutputModel>();
            outputWriter.RedirectToUrl(url);
            return DoNext.Stop;
        }
    }
and a convention to apply it to particular actions/handlers/endpoints:
    public class AuthenticationConvention: IConfigurationAction
    {
        public void Configure(BehaviorGraph graph)
        {
            graph
                .Actions()
                .Where(c => c.HasAttribute<SecureAttribute>())
                .Each(c => c.WrapWith<AuthenticationRequiredBehaviour>());
        }
    }
My guess is I need to wrap Login action with behaviour that will look up the previous url (the one hit before the login screen) and do a redirect. Is this the right direction or am I completely off here?

Gary Cox

unread,
Aug 2, 2012, 4:10:58 PM8/2/12
to fubumv...@googlegroups.com
We have on our login model a property of type string named ReturnUrl.  Fubu will wire this up when the login is loaded as long as there is a ReturnUrl in the querystring.  From there, on successful login we check if ReturnUrl is empty, if it is we redirect them to the home page, otherwise we redirect them to the ReturnUrl.

public FubuContinuation Execute(SignInModel model)
        {
            var loggedin = _authenticationService.SignIn(model.UserName, model.Password, true);

            if (loggedin)
            {
                return FubuContinuation.RedirectTo(model.ReturnUrl.IsEmpty() ? _urlRegistry.UrlFor<SomeHomeRequest>() : model.ReturnUrl);
            }

            return FubuContinuation.TransferTo(new SignInRequest { ReturnUrl = model.ReturnUrl, LoginFailed = true });
        }



--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/L5BEIqfs9bUJ.
To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.



--
Thank you,
Gary Cox

Jon Canning

unread,
Aug 2, 2012, 4:14:04 PM8/2/12
to fubumv...@googlegroups.com
That's what I've been doing, storing the last get url and model in the RequestDataProvider

--

Jesse Williamson

unread,
Aug 2, 2012, 4:21:27 PM8/2/12
to fubumv...@googlegroups.com
Took the words right out of my mouth.

Jon Canning

unread,
Aug 2, 2012, 4:27:39 PM8/2/12
to fubumv...@googlegroups.com
This is the behaviour:

gleb Chermennov

unread,
Aug 2, 2012, 9:41:13 PM8/2/12
to fubumv...@googlegroups.com
Thank you very much, will apply it as soon as I get to Visual Studio.

пятница, 3 августа 2012 г., 0:27:39 UTC+4 пользователь Jon Canning написал:
This is the behaviour:

To unsubscribe from this group, send email to fubumvc-devel+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.

--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-devel+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.

--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-devel+unsubscribe@googlegroups.com.

gleb Chermennov

unread,
Aug 6, 2012, 2:51:05 PM8/6/12
to fubumv...@googlegroups.com
That's the moment where I'm stuck - how to know in the behaviour if there actually is a url to return to after successful authentication? can I query BehaviourGraph somehow to get this url?

пятница, 3 августа 2012 г., 0:10:58 UTC+4 пользователь Gary L Cox Jr написал:
To unsubscribe from this group, send email to fubumvc-devel+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.

Jon Canning

unread,
Aug 6, 2012, 4:14:47 PM8/6/12
to fubumv...@googlegroups.com

How would there not be a url?

To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/9fuoMpeQ3AIJ.

To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.

Jesse Williamson

unread,
Aug 6, 2012, 4:31:59 PM8/6/12
to fubumv...@googlegroups.com
If you go the route that Gary suggested, your login controller takes a model that optionally holds the redirect URL. So you query the input model for it during your login. As for Jon's option, I'm not familiar with where RequestData comes from or it's lifetime, but I'm guessing it's coming from the IoC and has some kind of singleton scope since the idea is to check it for the redirect Url on the second request. Is that about right?
Reply all
Reply to author
Forward
0 new messages