2010-07-18 209 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應用程序池,它具有默認設置的「應用程序刷新」。 把它關掉......這應該解決你的問題。