(ASP.NET) How to determine how long a user has been on a site?

I need to display a warning to a user if they've been on my site for five minutes and haven't signed in.

How should I do it?

Can I add soemthing for the session to Application_Start? Is there a way to check the duration of the session? Perhaps put something on the title page / home page and if it is longer than five minutes, give a warning?

Any help would be appreciated.

Thank.

EDIT ---

What I ended up with was using the asp: Timer control found with AjaxControlToolKit.

+2


source to share


3 answers


The answer lies in Globals.asax, but Application_Start is wrong. This is used when the ASP.NET application actually starts.



I would add DateTime.Now to the session in the Session_Start method in Globals.asax. Then you can check this on every page load (for example, on the main page or on the main page load page) or use Ajax to poll the server.

+2


source


Application_Start is fired when the IIS application is initially loaded. Session_Start will start for every new session that starts.



If you are storing the current time in the session in Session_Start, you can check either the page load or the ajax call to determine if five minutes have passed without them logging in.

+7


source


If you really want a 5 minute validation, you need to register a 5 minute ajax callback with Session_Start. The tricky part is that the page is loaded and ready, which can receive it when it fires.

Polling the server with AJAX is probably the more common approach.

0


source







All Articles