Foosy
Foosy

Reputation: 31

How to detect when sessions expire in asp.net?

I want to track if users loout or their session expire. I need to k now exactly when their session expired (within +/- 5 min). Right now if a users leaves the page open in the browser and returns after a day, I will know it expired when he tries to do something after a day- but in reality the session expired after say an hour of inactivity.

I want to have a script run every 5 minutes on the server and check which sessions are expired and detect them and then log the expiration time in a db. How do I have a script run continuously with such short intervals on the server? What exactly does the script do? Is there a better way to do this?

Upvotes: 3

Views: 6466

Answers (1)

Heinzi
Heinzi

Reputation: 172200

In your global.asax, you can capture the Session_End event. See this question for further details:

Note that a five minute timeout is not easily possible, since you have to wait for the session to expire (= usually 20 min.) before you can detect the inactivity. Thus, if you really need the short interval, you'd need to

On the other hand, you also need to

  • make sure that five minutes of inactivity with an open browser does not cause the session to be abandoned (unless you want your users to lose their session when they leave the computer to get a cup of coffee). Such an "automatic keepalive" can be implemented, for example, by using JavaScript.

Upvotes: 10

Related Questions