Click here to Skip to main content
13,559,281 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 26 Nov 2009
Licenced CPOL

Session Timeout Notification

, 26 Nov 2009
Rate this:
Please Sign up or sign in to vote.
IntroductionSession management is a powerful concept in It’s provides 4 techniques to manage the session.In Proc (Default)– Most applications will use. State server SQL server – For scalable scenarios like web clustering etc. Custom (very rare) The beauty of sess


Session management is a powerful concept in It’s provides 4 techniques to manage the session.

  1. In Proc (Default)– Most applications will use.
  2. State server
  3. SQL server – For scalable scenarios like web clustering etc.
  4. Custom (very rare)

The beauty of session management in is without changing current coding we can use these management techniques in administration mode.

Please refer my blog entry for more details.


In any of the above cases, the session supposed to be timeout. It will handle by the runtime, which we can set in the web.config <sessionState timeout="1" />. Default timeout is 20 mins. Which means after 20 min session will become abandon and if you are not correctly handle it will leads to system crash. What general recommendation is, always check null of the session item that you’re going to refer. But in most user oriented application, we should notify the user that ,"Hello your current session has expired, what do you want to do?"

How to achieve this?

Using the code

If use In-Proc, it will automatically fire up session_end event which you can handle in Global.asax file. In rest of techniques this event will be ignored.

There are some workarounds to overcome this, but I’ll illustrate a simple trick, a pattern called “heartbeat”, that I’ve used in couple of my projects (especially in non AJAX applications).

In a nutshell, application will poll elapse time from the last post back and when it reaches to the time setup for session timeout, it will redirect to a page in which completely destroy the session.

Without talking much let’s see how it is working.

  1. By considering the session time out ,we will generate a javascript and register to the page. As you can see in the following code snippet, I’ve attached logout() to the windows.onload event. (In non AJAX case this is always true. ) After time has expired it will redirect to the Timeout.aspx page, where user can retry the application.
    ClientScriptManager cm = Page.ClientScript;
        StringBuilder sb = new StringBuilder();
        sb.Append("function logout(){");
        sb.Append("alert('Session has expired');");
        sb.Append("window.onload = function(){var t=setTimeout(\"logout()\",");
        // We have to convert into millisec.
        sb.Append(Session.Timeout * 1000*60);
        cm.RegisterStartupScript(Page.GetType(), "session_timout", sb.ToString(), true);
  2. Set up the Session Time out in the web.config
      <!-- set to 1 min for the sake of demonstration -->
      <sessionState timeout="1" />


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


About the Author

Kelum W. Ganegoda
Software Developer (Senior) Brandix Lanka Pvt Ltd.
Sri Lanka Sri Lanka
I’ve started my career in 2001 with Microsoft .net ver 1.0. I’m a MCSD for .net.

Currently, I’m working for Sri Lanka’s largest apparel exporter as a Software Engineer. All projects in .net, MS Sql Server, Biztalk Server, WCF and WPF. And also, I’m developing components to the ERP. In addition to that, I’ve involved to create architecture of ERP integration.

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 3 simple explanation and introduction Pin
Daniel Vincent16-Mar-11 21:37
memberDaniel Vincent16-Mar-11 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180515.1 | Last Updated 27 Nov 2009
Article Copyright 2009 by Kelum W. Ganegoda
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid