Click here to Skip to main content
Click here to Skip to main content

Tagged as

Session Timeout Notification

, 27 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
IntroductionSession management is a powerful concept in ASP.net. 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

Introduction

Session management is a powerful concept in ASP.net. 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 asp.net is without changing current coding we can use these management techniques in administration mode.

Please refer my blog entry for more details.

Background

In any of the above cases, the session supposed to be timeout. It will handle by the asp.net 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;
    if(!cm.IsClientScriptBlockRegistered(Page.GetType(),"session_timout"))
    {
        StringBuilder sb = new StringBuilder();
        
        sb.Append("function logout(){");
        sb.Append("alert('Session has expired');");
        sb.Append("window.location.href='http://www.codeproject.com/Timeout.aspx';");
        sb.Append("}");
        sb.Append("window.onload = function(){var t=setTimeout(\"logout()\",");
        // We have to convert into millisec.
        sb.Append(Session.Timeout * 1000*60);
        sb.Append(");}");
        cm.RegisterStartupScript(Page.GetType(), "session_timout", sb.ToString(), true);
    
    }
    
    
  2. Set up the Session Time out in the web.config
        <system.web>
                ...
          <!-- set to 1 min for the sake of demonstration -->
          <sessionState timeout="1" />
                ...
        </system.web>
    

License

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

Share

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.
Follow on   Twitter

Comments and Discussions

 
QuestionHow did you do this? [modified] Pinmemberjoeller11-Jun-12 11:32 
GeneralReason for my vote of 3 simple explanation and introduction PinmemberDaniel Vincent16-Mar-11 22:37 
Reason for my vote of 3
simple explanation and introduction
GeneralGood job Kelum! PinmemberMember 288490930-Jan-11 17:39 
Generallogin.aspx PinmemberAjay Kale New27-Sep-10 1:15 

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

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

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