formsauthenticationticket使用以前的登录数据进行解密

formsauthenticationticket使用以前的登录数据进行解密,authentication,cookies,asp.net-mvc,Authentication,Cookies,Asp.net Mvc,我有一个MVC应用程序,它使用Active Directory成员身份进行用户身份验证 用户登录后,将创建并加密FormsAuthenticatedTicket。然后,在Application\u PostAuthenticateRequest中,票据被解密,反序列化的用户数据存储在自定义主体对象中 我已将车票的过期日期从AddMinute(30)更改为AddMenute(10) 我的问题是,当用户在cookie过期后登录时,票证将使用当前登录数据加密,但在global.asax中,前一个登录的

我有一个MVC应用程序,它使用Active Directory成员身份进行用户身份验证

用户登录后,将创建并加密
FormsAuthenticatedTicket
。然后,在
Application\u PostAuthenticateRequest
中,票据被解密,反序列化的用户数据存储在自定义主体对象中

我已将车票的过期日期从AddMinute(30)更改为AddMenute(10)

我的问题是,当用户在cookie过期后登录时,票证将使用当前登录数据加密,但在global.asax中,前一个登录的票证仍然被解密。调试时,我看到同一用户的前一个过期日期(30分钟后而不是10分钟后)的票证数据,票证版本解密为2

我犯了什么错误


编辑: 我通过删除
FormsAuthentication.SetAuthCookie(用户名,rememberMe)解决了这个问题
因为我使用了FormsAuthenticationTicket对象

但我面临另一个问题: 在全局asax应用程序_PostAuthenticateRequest中,当尝试读取cookie时,我发现调试器为null,尽管cookie成功地添加了票据加密

string encryptedTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

        Response.Cookies.Add(cookie); 
我有两个用户:这个问题发生在其中一个用户身上,但另一个用户没有问题

请问,如何解决这个问题? Web.config:

 <authentication mode="Forms">
    <forms name=".ADAuthCookie" loginUrl="~/Account/Login">
    </forms>
 </authentication>

 <membership defaultProvider="ADMembershipProvider">  
   <providers>
     <clear/>
     <add name="ADMembershipProvider" 
          type="System.Web.Security.ActiveDirectoryMembershipProvider" 
          connectionStringName ="ADconnectionString"
           attributeMapUsername="sAMAccountName" />  

    </providers>  
 </membership>
全球asax

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    HttpCookie autoCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (autoCookie != null)
    {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(autoCookie.Value);
        Intervenant user = JsonConvert.DeserializeObject<Intervenant>(ticket.UserData/*, new JsonSerializerSettings {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects
        }*/);

        CustomADPrincipal customADPrincipal = new CustomADPrincipal(user); ;

        HttpContext.Current.User = customADPrincipal;
    }

}
受保护的无效应用程序\u PostAuthenticateRequest(对象发送方,事件参数e)
{
HttpCookie autoCookie=Request.Cookies[FormsAuthentication.formscookeName];
if(autoCookie!=null)
{
FormsAuthenticationTicket票证=FormsAuthentication.Decrypt(autookie.Value);
Intervenant user=JsonConvert.DeserializeObject(ticket.UserData/*,新的JsonSerializerSettings{
PreserveReferencesHandling=PreserveReferencesHandling.Objects
}*/);
CustomADPrincipal CustomADPrincipal=新CustomADPrincipal(用户);
HttpContext.Current.User=customADPrincipal;
}
}

问题的根源:我使用了渴望加载,因此userdata的长度更大,因此票据无法解密,这就是为什么global.asax中的cookie为null

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    HttpCookie autoCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (autoCookie != null)
    {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(autoCookie.Value);
        Intervenant user = JsonConvert.DeserializeObject<Intervenant>(ticket.UserData/*, new JsonSerializerSettings {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects
        }*/);

        CustomADPrincipal customADPrincipal = new CustomADPrincipal(user); ;

        HttpContext.Current.User = customADPrincipal;
    }

}