User.IsInRole doesn't work

后端 未结 16 1354
北恋
北恋 2020-12-05 18:29

I have ASP.NET MVC 4 application. I use Simple Membership Provider allowing to tick remember me checkbox under login form. If ticked, persitent cookie .ASPXAUTH is created w

相关标签:
16条回答
  • 2020-12-05 18:50

    If you're using IdentityManager by Brock Allen to create roles and assign them to users then you should read this article: https://github.com/IdentityManager/IdentityManager.AspNetIdentity/issues/3

    You'll need to uncomment the following line:

    this.RoleClaimType = System.Security.Claims.ClaimTypes.Role;
    

    Now you'll realize that your user that used to be in roles is no longer in them, and you'll have to re add them. If you take a look at the AspNetUserClaims table, you'll see claimType of Role and 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' you want the later.

    Once you do this, User.IsInRole("rolename") behaves as expected.

    I hope this helps someone, took me a while to figure this out.

    0 讨论(0)
  • 2020-12-05 18:54

    I had a smilar issue. In my case the problem solved when i log off and log in again.

    0 讨论(0)
  • 2020-12-05 18:54

    Paste this code in Global.asax

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
            {
                HttpCookie authCookie =
                    Context.Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie != null)
                {
                    FormsAuthenticationTicket authTicket =
                        FormsAuthentication.Decrypt(authCookie.Value);
                    string[] roles = authTicket.UserData.Split(new Char[] { ',' });
                    GenericPrincipal userPrincipal =
                        new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);
                    Context.User = userPrincipal;
                }
            }
    
            protected class RolesAttribute : AuthorizeAttribute
            {
                public RolesAttribute(params string[] roles)
                {
                    Roles = String.Join(",", roles);
                }
            }
    
    0 讨论(0)
  • 2020-12-05 18:59

    I had a similar issue, but in my case the problem was that the Session timeout was not synced with the authentication timeout, so I was not kicked out automatically but my session was being expired and, since my allowed operations were stored in a Session Variable, I was not able to retrieve the correct options for the current user.

    Try checking if your session is not expired even if you're still logged in

    0 讨论(0)
  • 2020-12-05 19:00

    None of these answers worked for me, but I did find the answer here: https://thinkthencode.wordpress.com/2016/04/24/azure-ad-using-app-roles-for-authorization/

    The key was adding the following to Startup.Auth.cs:

    TokenValidationParameters = new TokenValidationParameters
    {
        RoleClaimType = "roles"
    }
    
    0 讨论(0)
  • 2020-12-05 19:01

    Have you tried adding

    [InitializeSimpleMembership]
    

    in the controller where you're doing the check?

    0 讨论(0)
提交回复
热议问题