ASP.net: Session timeout prevention (keep alive).


/ Published in: ASP
Save to your folder(s)

Call AddKeepAlive() in Page_Load of the page that needs it. Change the path to the correct keep alive page.

Page should disable caching, like in second code snippet.


Copy this code and paste it in your HTML
  1. private void AddKeepAlive()
  2. {
  3. int int_MilliSecondsTimeOut = 2 * 60 * 1000; /*2 minutes*/ //Math.Max((this.Session.Timeout * 60000) - 30000, 5000);
  4. string path = VirtualPathUtility.ToAbsolute("~/KeepAlive.aspx");
  5.  
  6. string str_Script = @"<script>(function(){var r=0,w=window;if (w.setInterval)w.setInterval(function() {r++;var img=new Image(1,1);img.src='" + path + @"?count='+r;}," + int_MilliSecondsTimeOut.ToString() + @");})();</script>";
  7. Page.ClientScript.RegisterStartupScript(typeof(Page), UniqueID + "Reconnect", str_Script);
  8. }
  9.  
  10. //[KeepAlive.aspx:]
  11. <%@ OutputCache Location="None" VaryByParam="None" %>
  12. <?xml version="1.0" encoding="utf-8"?>
  13. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  15. <%=now %>
  16. </html>

URL: http://www.codeproject.com/KB/session/Reconnect.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.