views:

465

answers:

2

I'm trying to determine how much time is left in a given ASP.NET session until it times out.

If there is no readily available time-to-timeout value, I could also calculate it from its last access time (but I didn't find this either). Any idea how to do this?

A: 

You can get the timeout in minutes from:

Session.Timeout

Isn't this enough to provide the information, as the timeout is reset every request? Don't know how you want to display this without doing a request?

Anyhow, best way is on every request setting some Session variable with the last access time. That should provide the info on remote.

Jan Jongboom
+1  A: 

If you are at the server, processing the request, then the timeout has just been reset so the full 20 minutes (or whatever you configured) remain.

If you want a client-side warning, you will need to create some javascript code that will fire about 20 minutes from "now". See the setTimeout method.

I have used that to display a warning, 15 minutes after the page was requested. It pops up an alert like "your session will expire on {HH:mm}, please save your work". The exact time was used instead of "in 5 minutes" as you never know when the user will see that message (did he return to his computer 10 minutes after the alert fired?).

Hans Kesting
Such a solution only works if your application doesn't use more than one page at a time. What if the user opens a popup and clicks something there? The session will be "renewed" but the first page's counter time continues to run.
Mr Grieves