how to create a mechanism to notifi user his session has expired

Posted on 2008-11-04
Last Modified: 2013-12-17

whenever i go to my bank's website, and if the page is idled for a while, the next time when i clicked on it, it will tell me my session has expired.  can someone tell me programmatically how that's done.  

or even better would be that the page will change to show the session has expired, and that the user needs to login again.  
Question by:mmingfeilam
    LVL 6

    Accepted Solution

    LVL 6

    Assisted Solution


    What i have done for some of my projects is when the user logs in add a variable to the session.

    Session.Add("[variablename]", [variable]);
    You can add anything really, even jsut a simple string. Then for everypage in the Page_Load() check for that variable so:
    string s = Session("[variablename]").ToString;
    So if s is null then either the user hasn't logged in or the session has timed out so redirecdt them.

    Hope this helps

    Author Comment

    dwaynecharrington: i think you solution fits our need the best.  i want to modify it so that when it times out, as opposed to being redirected to a notification page, just have a javascript popup window, and when the user clicks on the ok button, redirects it to the default.aspx page.  but i want to make sure that the redirect really does reload/refresh the page so that any old session variables will be deleted (theoretically when session expires, these variables should be gone, but i am not sure).  

    Author Comment


    i made some modifications to the code you suggested:

    // for session timeout
    var elapsedTime = 0;
    // set to the Session.Timeout value to notify users
    // one minute after their Session times out.
    var maxTime;
    var oneSecond;
    function SetTimeOut()
        var hfTimeOut = document.getElementById("hfTimeOut");
        var timeOut = parseInt(hfTimeOut.value);
        maxTime = (timeOut + 1) * 60;
        oneSecond = window.setTimeout("timeoutCheck();",1000);

    // recursive function
    function timeoutCheck() {
        elapsedTime = elapsedTime + 1;
        if (elapsedTime > maxTime) {
            //window.location = "SessionTimeOutNotification.aspx";
            alert("Session Expired, Browser Will Now Be Closed. Please Open A New Browser To Login.");
        else {
          oneSecond = window.setTimeout("timeoutCheck();",1000);

    i noticed one problem, elapsedTime is never reset even if a user is active, normally, the session timeout is reset whenever there is a server-side request.  so the next question is, how do i notify this function of a timeout reset, which will take place server-side?

    also, has anyone ever use the glabal.asax event handler for Session_End?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now