Johan
Johan

Reputation: 35194

Get time left on session until timeout

Is there any way to know how long it is left on the session object before a timeout occurs?

I know how to get the actual timeout from the webconfig, but is there any way to know when the session was last active?

My users will be filling out large forms in my application, and if it takes more than 10 minutes before they hit "submit" they will be logged out. I would like to warn them if the session is about to timeout. I will make the check using an ajax call.

Upvotes: 7

Views: 20660

Answers (3)

watraplion
watraplion

Reputation: 287

We can handle this by defining the session time & warning time on the web.config and using the timer to check the elapsed time on the page.

Web.Config:

<appSettings>
  <add key="SessionWarning" value="30" /> 
</appSettings>

<system.web>
   <sessionState timeout="45"/>
</system.web>

Client Page :

<script language="javascript" type="text/javascript">
   /* Page Time Out Warning Message Script */
    var sessionTimeoutWarning = '<%=System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>';
    var sessionTimeout = "<%= Session.Timeout %>";
    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;

    var timeOnPageLoad = new Date();
    setTimeout('SessionWarning()', sTimeout);
    //To redirect to the home page
    setTimeout('RedirectToWelcomePage()', parseInt(sessionTimeout) * 60 * 1000);

    //Session Warning
    function SessionWarning() {
        //minutes left for expiry
        var minutesForExpiry = (parseInt(sessionTimeout) -
                                            parseInt(sessionTimeoutWarning));
        var message = "Your session will expire in another " + minutesForExpiry +
                                " mins! Please Save the data before the session expires";
        alert(message);
        var currentTime = new Date();
        //time for expiry
        var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes()
                                            + parseInt(sessionTimeout));

        //Current time is greater than the expiry time
        if (Date.parse(currentTime) > timeForExpiry) {
            alert("Session expired. You will be redirected to home page");
            window.location = "../Home.aspx";
        }
    }

    //Session timeout add home page where you want to redirect after session timeout
    function RedirectToWelcomePage() {
        alert("Session expired. You will be redirected to home page");
        window.location = "../Home.aspx";
    }
    /* End */

</script>

Upvotes: 5

TheKingDave
TheKingDave

Reputation: 976

You could also set a JavaScript time-out to the Session time-out via some code behind. Get it to run whatever JavaScript function you want. Very flexible and you don't need to muck about with checking the current time ... etc.

Something like this:

"window.setTimeout('yourJSFunctionGoesHere()', " + Session.Timeout + ");";

If you want to warn 2 minutes before your could does something like:

"window.setTimeout('yourWarningJSFunctionGoesHere()', " + Session.Timeout-2 + ");";

Upvotes: 5

nunespascal
nunespascal

Reputation: 17724

You could track the server time DateTime.Now in a javascript variable.
Compare that against the current time in javascript.

If the difference hits your threshold limit, you can show a warning message in javascript.

Better still, you could fire an ajax call to your server, and this would extend your server timeout.

Upvotes: 3

Related Questions