Session Management

Posted on 2007-07-28
Last Modified: 2012-06-21
How can i got message before session time out in 2.0(C#). I want to show a message box to user before session expire. Like "Your session going to expire, do you want to refresh?"
Question by:vikram_lashkari
    LVL 2

    Accepted Solution

    This function will generate a JS markup for the function based on the session time out set (10% of the time before the session timeout)

    public static void RegisterSessionJS(System.Web.UI.Page oPage)
                      //oPage.Session.Timeout = 1;
                      // 10% prior warning time
                      double sessionTimeOut = (oPage.Session.Timeout * 0.9) * 60000 ;
                      string sJS = "<SCRIPT Language=\"Javascript\">\n";
                      sJS += "window.setTimeout('SessionExpireWarning(" + sessionTimeOut + "," + (oPage.Session.Timeout * 60000) + ")'," + sessionTimeOut.ToString() + ");\n";
                      sJS += "</SCRIPT>\n";
                      if (!(oPage.ClientScript.IsStartupScriptRegistered("Session")))
                            oPage.ClientScript.RegisterStartupScript(oPage.GetType(),"Session",sJS );

    function SessionExpireWarning(nmSecs,ntotalmSecs){
          var endMS;
          var dtDate = new Date();
          var startMS = (((dtDate.getHours() * 60 * 60) + (dtDate.getMinutes() * 60) + dtDate.getSeconds()) * 1000) + dtDate.getMilliseconds();
                      if (window.confirm('Your session is about to expire. Do you wish to extend the Session?')){
                            dtDate = new Date();
                            endMS = (((dtDate.getHours() * 60 * 60) + (dtDate.getMinutes() * 60) + dtDate.getSeconds()) * 1000) + dtDate.getMilliseconds();
                            if ((endMS - startMS) >= (ntotalmSecs - nmSecs))
                                  alert('Sorry your session is expired. we cannot extend it ');
                                  sVirtualDir = (sVirtual.length > 0? sVirtual:'');
                                  window.showModalDialog('ExtendSession.aspx','','dialogHeight: 300px; dialogWidth: 400px;  edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll=No');
                                  window.setTimeout('SessionExpireWarning(' + nmSecs + ',' + ntotalmSecs + ')',nmSecs);

    I hope this will help.

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    737 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

    23 Experts available now in Live!

    Get 1:1 Help Now