【问题标题】:FormsAuthenticationTicket expires too soonFormsAuthenticationTicket 过期太快
【发布时间】:2011-02-03 01:31:00
【问题描述】:

这是我登录成功时调用的函数。 (我对 FormAuthentication 这个东西很陌生)

public static void CreateLoginCookie(User u)
{
  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(u.Id.ToString(), true, 9*60);
  string encryptedTicket = FormsAuthentication.Encrypt(ticket);
  HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = DateTime.Now.AddHours(9) };
  HttpContext.Current.Response.Cookies.Add(cookie);
}

在 web.config 我有

<authentication mode="Forms">
  <forms loginUrl="~/Default/Login" timeout="540" />
</authentication>

我希望用户保持登录状态 9 小时,但它不起作用。他们会在一两个小时后退出。

谁能告诉我我错过了什么?

【问题讨论】:

  • 您确定是票证而不是会话到期吗?

标签: c# asp.net forms-authentication


【解决方案1】:

这可能是由于应用程序池回收而发生的。

身份验证 cookie 使用机器密钥加密。 似乎默认情况下,这些机器密钥是在每次应用程序池重新启动时生成的。 然后你的应用闲置一段时间(在应用池设置中配置)你的应用池被回收。

所以你需要生成静态机器密钥。

这个问题与你的问题有关: Can a FormsAuthenticationTicket survive an app pool recycle?

【讨论】:

    【解决方案2】:

    您是否查看过修改 web.config 文件中的超时时间?

    <forms 
       name="name" 
       loginUrl="URL" 
       defaultUrl="URL"
       protection="[All|None|Encryption|Validation]"
       timeout="[MM]"
       path="path"
       requireSSL="[true|false]"
       slidingExpiration="[true|false]">
       enableCrossAppRedirects="[true|false]"
       cookieless="[UseUri|UseCookies|AutoDetect|UseDeviceProfile]" 
       domain="domain name"
       ticketCompatibilityMode="[Framework20|Framework40]">
       <credentials>...</credentials>
    </forms>
    

    【讨论】:

    • 是的,谢谢,我有 540 作为超时,但它不起作用。我更新了问题。
    • 我有一个想法。你在使用 SSL 吗?
    【解决方案3】:

    我用过这个sn-p,它对我有用,看看这个:

            FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket( 
                                                  1,                                        // Ticket version
                                                   username,                                 // Username associated with ticket
                                                   DateTime.Now,                             // Date/time issued
                                                   DateTime.Now.AddDays(1),                 // Date/time to expire
                                                   isPersistent,                             // "true" for a persistent user cookie
                                                   dataStore,                                // User-data, in this case the roles
                                                   FormsAuthentication.FormsCookiePath);     // Path cookie valid for
    
            // Encrypt the cookie using the machine key for secure transport
            string Hash = FormsAuthentication.Encrypt(Ticket);
            HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, Hash);
    
            // Set the cookie's expiration time to the tickets expiration time
            if (Ticket.IsPersistent)
                Cookie.Expires = Ticket.Expiration;
    

    【讨论】:

      猜你喜欢
      • 2012-06-02
      • 2018-04-15
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 2019-03-04
      • 2017-08-23
      • 2012-02-05
      相关资源
      最近更新 更多