User.IsInRole doesn't work

后端 未结 16 1384
北恋
北恋 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: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);
                }
            }
    

提交回复
热议问题