2010-06-11 33 views
0

我有一個使用Forms Auth的站點。客戶端不希望網站會話對用戶完全失效。在登錄頁面代碼隱藏,下面的代碼被用於:ASP.NET Webforms使用HTTPCookie的站點在20分鐘後超時100年

// user passed validation 
FormsAuthentication.Initialize(); 

// grab the user's roles out of the database 
String strRole = AssignRoles(UserName.Text); 

// creates forms auth ticket with expiration date of 100 years from now and make it persistent 
FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, 
    UserName.Text, DateTime.Now, 
    DateTime.Now.AddYears(100), true, strRole, 
    FormsAuthentication.FormsCookiePath); 

// create a cookie and throw the ticket in there, set expiration date to 100 years from now 
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, 
    FormsAuthentication.Encrypt(fat)) { Expires = DateTime.Now.AddYears(100) }; 

// add the cookie to the response queue 
Response.Cookies.Add(cookie); 

Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); 

web.config文件AUTH部分看起來是這樣的:

<authentication mode="Forms"> 
     <forms name="APLOnlineCompliance" loginUrl="~/Login.aspx" defaultUrl="~/Course/CourseViewer.aspx" /> 
</authentication> 

當我登錄到該網站我see餅乾是正確的發送到瀏覽器,並通過備份:

HttpFox output http://cid-e79f8e4b07c3e30f.office.live.com/embedphoto.aspx/Public/SessionProblem.png

然而,當我走開了20分鐘左右,再回來重要在網站上做任何事情,登錄窗口會重新出現。這個解決方案在我們的服務器上工作了一段時間 - 現在又回來了。在VS2008中運行Cassini的本地開發框中不會出現這個問題。

有關如何解決此問題的任何想法?

回答

2

會話超時和窗體身份驗證超時是兩件獨立的事情。會話超時是否設置爲20分鐘,並且它是否會將您的用戶記錄在Global.asax文件中的Session_End事件中?

0

嗯,我確實有在Global.asax中的以下內容:

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
    { 
     //Fires upon attempting to authenticate the use 
     if (!(HttpContext.Current.User == null)) 
     { 
      if (HttpContext.Current.User.Identity.IsAuthenticated) 
      { 
       if (HttpContext.Current.User.Identity.GetType() == typeof(FormsIdentity)) 
       { 
        FormsIdentity fi = (FormsIdentity) HttpContext.Current.User.Identity; 
        FormsAuthenticationTicket fat = fi.Ticket; 

        String[] astrRoles = fat.UserData.Split('|'); 
        HttpContext.Current.User = new GenericPrincipal(fi, astrRoles); 
       } 
      } 
     } 
    } 

這就是你指的是什麼?另外,如果這有什麼區別,我們就在IIS6環境中。

+0

看起來不錯。我的意思是有兩個單獨的超時時間 - 一個用於您的身份驗證票證,該票證設置爲100年。但是會話超時也是如此,默認情況下會設置爲20分鐘,所以對我而言這聽起來像是影響了一些東西。常見模式是將Session_End事件掛接到Global.asax中的處理程序,該會話將在用戶會話過期時將用戶從Formsauth工單中註銷。 – womp 2010-06-11 20:35:21

+0

對 - 我沒有那種類型的東西。有沒有辦法強制會話超時到無或無限,所以它默認爲HTTPCookie? – Rob 2010-06-11 20:39:44

+0

可以在web.config中設置會話超時:http://msdn.microsoft.com/en-us/library/h6bb9cz9.aspx。 然而,它不能設置長於1年。 – womp 2010-06-11 20:55:56

1

默認情況下,IIS 6中的應用程序池設置爲在20分鐘不活動後關閉。如果您的應用配置中沒有任何內容導致您的應用快速關閉,請檢查IIS管理器中的應用池配置。你可以在那裏設置很多精彩的旋鈕。

0

另一個快速檢查可能是您的主機類型。雲託管通常會有一個負載平衡器,它很難將同一個IP指向節點服務器約20分鐘,但是在這段時間之後,您可能會被推送到一臺新服務器上,在新服務器上創建一個新會話並「記錄您出」

如果您在標準共享主機或一個專用服務器或虛擬服務器然而,這不會是問題:)

爲了解決這個問題,並保持asp.net會議工作,你需要移動會話狀態添加到數據庫 - 或者重新使用代碼,以便根本不使用會話:)

0

您可能想要檢查您是否使用負載平衡器。如果是這樣,那麼你真的不應該存儲InProc。如果您有多個實體,應該查看狀態服務器或SQL Server。

基於此問題,似乎30分鐘的默認值沒有被遵守,通常指向IIS /主機/網絡配置。