Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 24, 2013 @ 12:20
    Dan Diplo
    0

    How to check whether a member is logged in AND in the correct role?

    In Umbraco I've created a protected page [using User Permissions] that uses role-based permission to only allow access to people in the group "WebsiteMember".

    I've then written an MVC surface controller to authenticate the user. This is basically:

    public class LoginSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
        [HttpPost]
        public ActionResult ProcessLogin(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
    
            if (Membership.ValidateUser(model.Username, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Username, model.StayLoggedIn);
    
                if (!String.IsNullOrEmpty(model.RedirectToUrl))
                {
                    return Redirect(model.RedirectToUrl);
                }
    
                return RedirectToCurrentUmbracoPage();
            }
            else
            {
                ModelState.AddModelError(String.Empty, "Your username or password was not recognised.");
                return CurrentUmbracoPage();
            }
        }
    

    This all works fine - so long as the person attempting to log in is in the correct role group for the protected page.

    The problem I have is when someone attempts to login who has a valid username and password but is not in the correct role group for the protected page.

    What basically happens is this:

    Membership.ValidateUser(model.Username, model.Password) will return true and the forms authentication cookie gets set. This means that User.Identity.IsAuthenticated is now true. As far as ASP.NET is concerned the person is authorised. However, Umbraco won't let them access the page because they are not in the correct role group.

    How can I detect this and inform the user that they are logged in but are not in the correct role group to access the page?

     

  • uWebshop 35 posts 140 karma points
    Oct 24, 2013 @ 12:47
    uWebshop
    0

    I think you want to use:

    System.Web.Security.Roles.IsUserInRole(string userName, string roleName)
    
  • Jamie Howarth 306 posts 773 karma points c-trib
    Oct 24, 2013 @ 12:49
    Jamie Howarth
    0

    Hi Dan,

    See here: Roles.IsUserInRole will use the role provider to check whether your member is in the correct group, at which point you can either:

    • Not set the auth cookie, boot the user and force them to try & log in again, or;
    • Add ViewData to your view which then shows a nice friendly error to the user, informing them they have insufficient permissions.
    HTH,
    B
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 24, 2013 @ 13:11
    Dan Diplo
    0

    Thanks to both of you. But... how do I know which role to check? 

    Consider this: There are a number of role groups set up. Let's call them "GroupA", "GroupB" and "GroupC".

    An Umbraco editor protects a page - let's call it "Page One" - and protects it using role-based permissions so the authenticated user has to be in "GroupB". But another editor protects "Page Two" and this time sets the role-based permission to "GroupA".

    So in my generic login controller how do I know which role group to check the user against? That requires knowledge of what role the page has been protected with. Do you see the problem?

  • uWebshop 35 posts 140 karma points
    Oct 24, 2013 @ 13:31
    uWebshop
    0

    Dan,

    Did you already try to get the data from the umbracocontext? You can just use this in your action result.

    UmbracoContext.Security.IsMemberAuthorized()
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 24, 2013 @ 15:09
    Dan Diplo
    0

    Thanks, never new about the Security class. But... still doesn't seem to get me exactly what I want. It appears that UmbracoContext.Security.IsMemberAuthorized() seems to be the same as User.Identity.IsAuthenticated ie. it returns true if someone is logged in, but doesn't take into account whether they are in the role group required to access the page being viewed. 

    Maybe there just isn't a way?

  • Charles Afford 1163 posts 1709 karma points
    Oct 26, 2013 @ 17:12
    Charles Afford
    100

    In the app data of your solution you will have an access config.

    Its an XML file.  This will give you the role and permissions on the node.

    You can then check the role the member is in and check it against the node id in this access config.

    If you find a match they have access if you do not then they dont :).  Charlie :)

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 27, 2013 @ 20:54
    Dan Diplo
    0

    Good thinking, Charles - I'd forgotten about that. I was really hoping there'd be a nicer way, though, using an existing API. But this will have to do - thanks!

Please Sign in or register to post replies

Write your reply to:

Draft