1

是否可以在 ASP.net 中使用 sessionState 获取剩余会话超时

这是我的 webconfig 文件中的 sessionState 代码。

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

谢谢

4

2 回答 2

1

我知道这是一篇旧帖子,但没有正确答案,我需要这个答案。

我已经在某种程度上使用了 Ashwin 的答案并使它起作用!

您需要在开始会话和开始会话的位置创建 session["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);
            }
于 2014-07-10T11:00:18.603 回答
-6

是的,您可以使用以下代码获取剩余时间

    HttpSessionState session = HttpContext.Current.Session;//Return current sesion
    DateTime? sessionStart = session[SessionKeys.SessionStart] as DateTime?;//Convert into DateTime object
    if(sessionStart.HasValue)//Check if session has not expired
    TimeSpan remaining = sessionStart.Value-DateTime.Now ;//Get the remaining time
于 2012-07-11T04:10:18.540 回答