Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time left on session until timeout

Tags:

c#

asp.net

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.

like image 259
Johan Avatar asked May 08 '13 09:05

Johan


People also ask

Is it possible to manually set a session out timer?

Yes, we can set the session timeout manually in web. config. In ASP.NET we can set the session timeout in the web. config file.

Can we set session timeout?

There are two ways to set a session timeout in ASP.NET. First method: Go to web. config file and add following script where sessionstate timeout is set to 60 seconds.

What is session timeout period?

Session timeout represents the event occuring when a user does not perform any action on a web site during an interval (defined by a web server). The event, on the server side, changes the status of the user session to 'invalid' (ie.


2 Answers

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 + ");";
like image 83
TheKingDave Avatar answered Oct 06 '22 08:10

TheKingDave


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.

like image 28
nunespascal Avatar answered Oct 06 '22 09:10

nunespascal