2012-07-11 51 views
1

是否有可能通過ASP.net中的sessionState獲取REMAINING會話超時如何使用SessionState獲取剩餘的會話超時?

以下是我的webconfig文件中的sessionState代碼。

<sessionState 
    mode="SQLServer" 
    allowCustomSqlDatabase="true" 
    sqlConnectionString="my connection here" 
    timeout="100"> 
</sessionState> 

感謝

+0

每次頁面加載時會不會重置會話超時?每當您有機會檢查剩餘時間時,它將剛剛重置爲100. – josh3736 2012-07-11 04:13:23

回答

-3

是的,你可以使用下面的代碼來獲取剩餘時間

​​
+2

我出現錯誤,SessionKey在當前上下文中不存在 – user1120260 2012-07-11 04:36:39

+0

是的,什麼是SessionKeys? – AntonK 2014-04-30 06:09:58

0

我知道這是一個老的文章,但沒有正確的答案,我需要這樣回答。

我已經使用了Ashwin的答案,並使其工作!

您需要創建會話[「SessionStart」]變量,您可以隨時開始會話並開始會話。從那裏它是非常簡單的,我返回的秒作爲值,所以我可以使用javascript來改變秒爲hh:mm:ss,我還包括一個string.format將秒轉換成hh:mm:ss。

  // Get the session in minutes and seconds 
      HttpSessionState session = HttpContext.Current.Session;//Return current session 
      DateTime? sessionStart = session["SessionStart"] as DateTime?;//Get DateTime object SessionStart 
      //Check if session has not expired 
      if (sessionStart.HasValue) 
      { 
       TimeSpan timepassed = DateTime.Now - sessionStart.Value;//Get the time passed since the session was created 

       int TimeOut = (session.Timeout * 60) - Convert.ToInt32(remaining.TotalSeconds); 
       int h = (TimeOut/3600); 
       int m = ((TimeOut/60) % 60); 
       int s = TimeOut % 60; 

       SessionTimeoutValue.InnerText += TimeOut; // or use the string.format below. 
       //string.Format("{0}:{1}:{2}", 
       // h, 
       // m, 
       // s); 
      } 
相關問題