text: You are not always have a luxury to bump up the Session expiration period from 20 minutes on a server (most likely you are on shared hosting). Plus it's a bad idea to unnecessary consume the resources on a server. I have a simple solution that will take care of that problem.
  The solution is simple. Lets create an aspx page that will return 1x1 transparent gif image and with javascript refresh it every 15 minutes. Thus the browser will renew the session every 15 minutes if user simply reading long text on a page or walked away to get a cup of coffee. But as soon as browser closed (or user navigated away from our site) the Session will expire right after 20 minutes of inactivity. And we do not consume the resources on a server.
  1. Create renewSes.aspx page
  2. In code behind have following lines
  public class renewSes : System.Web.UI.Page {}; override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { Response.AddHeader("ContentType", "image/gif"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(gif); Response.End(); } }This code will simply output small 1x1 transparent gif which is in the array "gif" so we do not need to hit the file system for that. Plus the Cacheability is set to NoCache, we do not want browser to hit cache, we want it to renew our session.
  3. Create user control that has following HTML
  <script language="Javascript"> window.setInterval("renewSession();", 600000); function renewSession() {document.images("renewSession").src = "/renewSes.aspx?par=" + Math.random();} </script> <img src="/renewSes.aspx" name=renewSession id=renewSession>The code simply hits /renewSes.aspx every 10 minutes. Thus keeping your session alive. Plus the Math.random was added to avoid caching in the browser.
  4. Dump it on every page somewhere. Or if you are using templates then simply add it to your master page.
  5. Done!!!!!!. You might want to move the JavaScript code to RegisterClientScriptBlock but it's not necessary
  Obviously this method will only work if JavaScript is on but it's a majority, plus do not forget it's only for rare cases when user leaves your page open and walks away expecting you will keep him logged in forever. The Server resources are minimal since the sessions will continue to expire every 20 minutes when user navigated away and the only excessive traffic is those 48 bytes every ten minutes to keep the session alive.
  If you want to see how it works feel free to visit my E-Commerce Body jewelry site. Add something to the shopping cart and wait for 20 minutes or for couple days. When you will be back the cart still going to be there.
  General
  News
  Question
  Answer
  Joke
  Rant
  Admin