为什么我的表单身份validation票证到期如此之快?

我在ASP.NET应用程序中使用表单身份validation。 我将FormsAuthenticationTicket配置为在1年后到期,但它实际上在1小时后到期。 我无法弄清楚为什么。

以下是登录过程中涉及的所有代码:

 public static bool Login(int id) { try { string securityToken = UserHelper.AuthenticateUser(id); DateTime expiryDate = DateTime.Now.AddYears(1); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, id.ToString(), DateTime.Now, expiryDate, true, securityToken, FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.Expires = expiryDate; HttpContext.Current.Response.Cookies.Add(cookie); return true; } catch { return false; } } 

Web.config文件:

       ... 

我的方法有问题吗? 为什么它到期如此之快?

编辑

Global.asax代码:

 protected void Application_AuthenticateRequest(object sender, EventArgs e) { if (Request.PhysicalPath.EndsWith(".aspx") || Request.PhysicalPath.EndsWith(".axd") || Request.PhysicalPath.EndsWith(".ashx")) SecurityManager.SetPrincipal(); } 

SetPrincipal代码:

 public static void SetPrincipal() { ILivrePrincipal principal = null; FormsIdentity identity; UrlParameters urlParameters = UrlParametersHelper.GetUrlParameters(HttpContext.Current.Request); if (HttpContext.Current.Request.IsAuthenticated) { identity = (FormsIdentity)HttpContext.Current.User.Identity; User userProfile; urlParameters.SecurityToken = (((FormsIdentity)identity).Ticket).UserData; try { userProfile = UserHelper.GetUser(urlParameters.SecurityToken); UserHelper.UpdateLastActiveOn(userProfile); principal = new AuthenticatedPrincipal(identity, userProfile); } catch { //TODO: Log an exception FormsAuthentication.SignOut(); principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null)); } } else { principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null)); } HttpContext.Current.User = principal; } 

这是你的问题。

  

每次应用程序池回收时,ASP都会生成一个新的机器密钥。 每小时都可能发生这种情况。

机器密钥用于加密和解密FormsAuthentication cookie。 如果它发生变化,浏览器上的cookie就不再有用了。 因此系统会将您视为您从未登录过。

尝试生成静态密钥并将其添加到配置文件中。 应该看起来像这样:

  

在这里生成一把钥匙。

我没有看到代码有什么问题。 您使用的是什么浏览器,或许它不能识别1年的到期日期? 我会用fiddler或一些这样的工具查看响应头,看看实际发送的是什么。

这可能有助于http://support.microsoft.com/kb/910439/

我的猜测是cookie在票证到期之前到期。 上面的文章向您展示了调试的方法,以确定是否确实如此。

我能看到的唯一不标准的是你将id.ToString()传递给FormsAuthenticationTicket构造函数。 我通常在这个参数中传递用户名。 不确定这是否会有所作为,但值得一试。

您是否在应用程序中使用可能导致超时的任何其他内容? 例如,如果进程内会话状态到期,则自动将您注销。

我假设你的Global.asax中有一些代码来处理经过身份validation的请求吗?