Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to auto logout a user session based on the session timeout regardless of Activity or Inactivity. Also how can we show a session timeout timer on the page.


Thanks.
Posted

You have to set your session timeout. Read ASP.NET Session State[^]
 
Share this answer
 
Hold on, the whole idea of session timeout is to end the session once there is no activity. Or do you want to expire the session after a certain time user has spent on your page/website?

Anyways, Look at the following links for inspiration to show timer on your webpage.

http://programmerramblings.blogspot.nl/2009/08/aspnet-session-timeout-control-jquery.html[^]

http://www.misfitgeek.com/2010/02/session-time-out-tricks/[^]
 
Share this answer
 
 
Share this answer
 
Here is the complete solution,

1. You have to set the session timeout in web.config
2. In page load, add the code below
3. Add the client side cod to popup the alert


protected void Page_Load(object sender, EventArgs e)
{
        int SessionTimeout = Session.Timeout;
        StringBuilder d = new StringBuilder();

        d.Append("<script language='javascript'>\n");
        d.Append("function BeforeTimeout()\n{\n");
        d.Append("setTimeout('CheckSession()',60000*" + ")\n}\n");
        d.Append("\n");
        d.Append("function CheckSession()\n{\n");
        d.Append("doLogout();\n}");
        d.Append("window.onload=BeforeTimeout; \n");
        d.Append("</script>\n");

        Page.ClientScript.RegisterStartupScript(typeof(Page), "key1", d.ToString());
}


client side
function doLogout(){
   //call ajax to fire server side method
   //in server side methid - remove all session and redirect to login page
}


Hope this helps.
cheers
 
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