Does a Session timeout Generate an event?

  • Thread starter Thread starter David Laub
  • Start date Start date
D

David Laub

Is it possible to know when an asp.net session has timed out? Is there some
sort of timer event?

Thanks
 
Put your code in the

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub

handler of the global.asax file.

Same as in the ASP classic.
 
Session_OnEnd() in your global.asax file is called whenever the session
times out (or is abandoned explicitly).

-- Alex Papadimoulis
 
Thanks - but that just raises what I guess should have been my REAL
question:

Is there a way of detecting a "real" time out - i.e. the user of the web app
still has a page open in his/her browser, but didn't click a post-back for
20 minutes
versus the user explicitly closed the page/browser

what I specifically want is to display an error page on a time out - but if
just put unconditional code in Session_End, I'll have no way of knowing
what's an error and what isn't.
 
David,

That can only be handled with client-side code. You'll need to use the
SetTimeout Javascript function. You'll probably need a work around to get it
to handle 20+ minutes, since i believe it works in miliseconds. But, that's
not a terrible deal, I'm sure you could figure it out.

--Alex Papadimoulis
 
This is how I would do it.

Craft a cookie that expires, say, in 30 minutes (i.e., a non-persistent
cookie that times out later than a session cookie).

If your Session_start even handler, look for this cookie. If it is present,
then it indicates that user has timed out, but haven't closed the browser.

If the cookie doesn't exist, either it is because the user has not posted
back for 30 mins or the user closed browser (which would remove a
non-persistent cookie).

I guess you just have to determine what is an optimum time for the cookie
expiry time in order to distinguish between time-out vs browser closing.

HTH
 
Back
Top