C# 角色在FormsAuthentication中停止工作

C# 角色在FormsAuthentication中停止工作,c#,asp.net,.net,authentication,webforms,C#,Asp.net,.net,Authentication,Webforms,从.net 3.5版升级到4.5版后,FormsAuthentication中的角色停止工作。用户已通过身份验证,但框架似乎没有获取角色信息,并且用户被拒绝访问管理内容 以下是登录用户的代码: int timeout = int.Parse(ConfigurationManager.AppSettings["loginTimeoutMinutes"]); HttpContext.Current.Session.Timeout = timeout;

从.net 3.5版升级到4.5版后,
FormsAuthentication
中的角色停止工作。用户已通过身份验证,但框架似乎没有获取角色信息,并且用户被拒绝访问管理内容

以下是登录用户的代码:

        int timeout = int.Parse(ConfigurationManager.AppSettings["loginTimeoutMinutes"]);

        HttpContext.Current.Session.Timeout = timeout;

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,
            username,
            DateTime.Now,
            DateTime.Now.AddMinutes(timeout),
            false,
            roles,
            FormsAuthentication.FormsCookiePath);

        string hash = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(
            FormsAuthentication.FormsCookieName, // Name of auth cookie
            hash); // Hashed ticket

        if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

        HttpContext.Current.Response.Cookies.Add(cookie);
在Global.asax中,此代码使用角色信息更新当前用户。调试时,我可以看到角色是admin:

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated &&
                HttpContext.Current.User.Identity is FormsIdentity)
            {

                FormsIdentity id = (FormsIdentity) HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;

                string userData = ticket.UserData;
                string[] roles = userData.Split(',');
                HttpContext.Current.User = new GenericPrincipal(id, roles);
            }
        }
    }
以下是主web.config的身份验证位:

  <roleManager enabled="true"></roleManager>
  <authentication mode="Forms">
    <forms name="theForm" loginUrl="/login.aspx"/>
  </authentication>
当我调试时,一切看起来都很好,但是用户被拒绝访问管理页面

我错过什么了吗?谢谢你的帮助。(我知道以前有人问过这个问题,但我在stackoverflow上阅读了大约50个问题/答案,并尝试了所有建议,但没有找到答案)

(另一件奇怪的事情是,升级.net版本后,我不得不添加

<add key="enableSimpleMembership" value="false"/>


转到web.config以获得重定向以转到正确的登录页。)

Stackoverflow的好人

我使用了错误的
EventHandler
。要使用的是
PostAuthenticateRequest

我真诚地向18位读了问题的人道歉,我浪费了他们的时间

<add key="enableSimpleMembership" value="false"/>