Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have this code on my masterpage
however it will always expire regardless if it is inactive or not

must detect when user is not doing activity like keypress, clicks etc. and if does must reset timer to avoid being loggedout.


C#
protected void Page_PreRender(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 5)) + "; Url=./Public/SessionExpired.aspx");
    }



web.config:

<sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>

how do I check if user is idle or inactive in ASP.NET
Posted
Updated 11-Jan-11 6:10am
v2

What is your definition of idle or inactive? Do you mean the user has not made a request to your site in a given period?

From the server, there is no way to tell if the user has browsed to another site or walked away from the screen. As long as they continue to make requests, they are active. You can simulate this by using JavaScript to send a heartbeat but once they leave your site, there is nothing you can do.
 
Share this answer
 
Comments
gherard 11-Jan-11 11:52am    
it is inactive if there is no clicks or no keypresses within the page.
[no name] 11-Jan-11 13:56pm    
Right. There is no way to tell if this inactivity is because the user is reading the page, has walked away from the screen or has navigated to another site.
with the help of javascript it is possible. There can be a call to a script on each page of application. The javascript function will notice the clicks and key presses. On each click and key press it will reset its count to zero. So in case user does not click or presses any key for a time the count will begin to increase after a particular time interval. after certain value of count we can show message to client that he is sitting idle for last couple of minutes.
 
Share this answer
 
Comments
gherard 11-Jan-11 11:57am    
how do i achieve this in javascript?
As Mark says, it is impossible to tell what the user is doing, but you can give a session time out.
Here[^] is a Stack Overflow link.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900