• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Session Management

How can i got message before session time out in asp.net 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?"
0
vikram_lashkari
Asked:
vikram_lashkari
1 Solution
 
active_agentCommented:
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 ');
                              window.location.reload();
                        }
                        else{
                              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.




0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now