2010-07-18 207 views
3

我尝试重新登录后的一天后,我的Web应用程序身份验证Cookie超时。 我试图通过诺基亚浏览器和Internet Explorer访问应用程序,两者具有相同的行为。ASP.NET MVC身份验证Cookie

这是我的登录过程:

[HttpPost] 
    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", 
         Justification = "Needs to take same parameter type as Controller.Redirect()")] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (MembershipService.ValidateUser(model.UserName, model.Password)) 
     { 
      //FormsService.SignIn(model.UserName, model.RememberMe); 
      FormsAuthenticationTicket Authticket = new 
         FormsAuthenticationTicket(1, 
         model.UserName, 
         DateTime.Now, 
         DateTime.Now.AddYears(1), 
         true, 
         "", 
         FormsAuthentication.FormsCookiePath); 

      string hash = FormsAuthentication.Encrypt(Authticket); 

      HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 

      if (Authticket.IsPersistent) Authcookie.Expires = Authticket.Expiration; 

      Response.Cookies.Add(Authcookie); 

      if (!String.IsNullOrEmpty(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      return RedirectToAction("Index", "Home"); 
     } 
     ModelState.AddModelError("", "The user name or password provided is incorrect."); 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

web.config设置:

<forms loginUrl="~/consignment/Account/LogOn" timeout="2880" protection="All" name=".consignmentauthadmin"/> 

我想:

[HttpPost] 
    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", 
        Justification = "Needs to take same parameter type as Controller.Redirect()")] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (MembershipService.ValidateUser(model.UserName, model.Password)) 
     { 
      //FormsService.SignIn(model.UserName, model.RememberMe); 
      FormsAuthenticationTicket Authticket = new 
         FormsAuthenticationTicket(1, 
         model.UserName, 
         DateTime.Now, 
         DateTime.Now.AddYears(1), 
         true, 
         "", 
         FormsAuthentication.FormsCookiePath); 

      string hash = FormsAuthentication.Encrypt(Authticket); 

      HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 

      if (Authticket.IsPersistent) Authcookie.Expires = Authticket.Expiration; 

      Response.Cookies.Add(Authcookie); 

      if (!String.IsNullOrEmpty(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      return RedirectToAction("Index", "Home"); 
     } 
     ModelState.AddModelError("", "The user name or password provided is incorrect."); 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

我不想认证到期,直到我从应用程序中注销。我究竟做错了什么?

+0

我没有看到任何问题,也许你需要打开你的cookie,并通过浏览器阅读它,看看你有什么数据,如果日期是正确的。然后,也许你的浏览器删除它,或找不到它。 – Aristos 2010-07-19 08:55:59

+0

我已经阅读了cookie,这是我8小时前收到的信息。 Cookie的详细信息: 截止日期:20/07/2011 12:00:00 a.m. 过期库存:False 今天早上我再次尝试时,cookie已过期。 Web应用程序正在虚拟目录下运行,因此它可能会从根目录中获取到期日期? – ace 2010-07-19 22:27:47

回答

2

尝试查看您的网络服务器IIS应用程序池,它具有默认设置的“应用程序刷新”。 把它关掉......这应该解决你的问题。