1

ASP.netのsessionStateを使用してREMAININGセッションタイムアウトを取得することは可能ですか?

これが私のwebconfigファイルのsessionStateコードです。

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

ありがとう

4

2 に答える 2

1

I know this is an old post but there is no correct answer and I needed this answered.

I have used Ashwin's answer somewhat and made it work!

You need to create the session["SessionStart"] variable where ever you start your session and begin your session. From there it is pretty straight forward, I am returning seconds as the value so I can use javascript to change the seconds to hh:mm:ss, I have also included a string.format to convert the seconds into 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 に答える