2013-09-16 41 views
0

我的會話超時在我的託管環境中非常短,有時甚至是2秒,並且他們超時。在主機環境中會話超時時間很短?測試會話丟失?

的會話,重新如果用戶繼續使用該網站,除非會話= null且計數爲0

的會議應20分鐘後超時,然後將用戶重定向到登錄頁面

的代碼,這是下面:

protected override void OnInit(EventArgs e) 
{ 
    if (this.Session != null && this.Session.Count > 0) 
    { 
     string email = (string)this.Session["Email"]; 
     int practiceId = (int)this.Session["PracticeId"]; 
     int practitionerId = (int)this.Session["PractitionerId"]; 

     this.ClientScript.RegisterHiddenField("loggedInUserName", email); 
     this.ClientScript.RegisterHiddenField("practiceId", practiceId.ToString()); 
     this.ClientScript.RegisterHiddenField("practitionerId", practitionerId.ToString()); 
    } 
    else 
    {  
     this.Session.Abandon(); 
     Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", "")); 
     Response.Redirect("~/Default.aspx");  
    } 

    base.OnInit(e); 
} 

有誰知道爲什麼我的會話超時可能是如此之短? 當我有時間使用我的網站時,我可以在沒有超時的情況下移動2-5分鐘,而其他時間在10秒鐘內我可以暫停。什麼會導致會話丟失,他們是否有任何方法來避免或測試會話丟失?

在此先感謝。

回答

0

我假設你正在重寫頁面的初始化函數,但可能放棄每個頁面加載的會話可能會導致更多的問題,而不是解決的問題。我會檢查母版頁內的會話的存在:

protected void Page_Init(object sender, EventArgs e) 
{ 
    if (!HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
     // user is not logged in 
     string ReturnUrl = HttpContext.Current.Request.Url.PathAndQuery; 
     string RedirectUrl = "/Login.aspx"; 
     if (!String.IsNullOrEmpty(ReturnUrl)) 
     { 
      RedirectUrl += "?ReturnUrl=" + Server.UrlEncode(ReturnUrl); 
     } 
     Response.Redirect(RedirectUrl); 
    } 
} 

如果這是在母版頁它會檢查每個請求(於從主繼承一個aspx頁面進行),將重定向用戶登錄頁面。

如果您的應用共享一個應用程序池,你可能會與其他應用程序共享的Cookie ID:

<authentication mode="Forms"> 
    <forms loginUrl="~/Login.aspx" timeout="60" name="MY_COOOKIE_NAME" slidingExpiration="true" /> 
</authentication> 
<sessionState timeout="60" /> 

的MY cookie名稱將確定應用程序使用的餅乾,其他應用程序可能有默認的Cookie名稱,所以您的會話雖然明顯通過身份驗證,但不屬於應用程序,因爲它們正被其他應用程序覆蓋。滑動過期意味着您的會話時間將在您每次訪問頁面時得到延長。

此外,檢查的machineKey配置元素存在,這使我的會議更穩定:

<machineKey 
validationKey="random_validation_key" 
decryptionKey="random_decryption_key" 
validation="SHA1" decryption="AES" />