Posted
on Feb 1, 2011
(permalink)
We are trying to implement a functionality which gives a warning to the user before Session Expires
Below is the master page we are using
When I run the code below in local machine (Session State is inproc) it works as expected and gives the warning to the user and if there is still inactivity after 30 secs it redirects to the Login page.
But in our development machine which uses Sql server for Session State , after warning the user and if there is still inactivity session never expires . Is it possbile that a javascript alert would be considered as "Activity" so that the session doesnt gets inactivated? IF so how come it works when the session state is inproc?
Thanks in advance
Master Page:
<body>
<form id="Form1C" runat="server" enctype="multipart/form-data">
<telerik:RadScriptManager ID="sm1" runat="server" EnableCdn="true">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js"/>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js"
Path="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"/>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryPlugins.js"/>
<asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"/>
<asp:ScriptReference Path="~/Resources/js/Common.js" />
</Scripts>
</telerik:RadScriptManager>
public partial class Master1C : System.Web.UI.MasterPage
{
private void CheckSessionTimeout()
{
string msgSession = "Warning: Within next 30 secs., if you do not do anything, " + "our system will redirect to the login page. Please save changed data.";
//time to remind, 30 secs before session ends
int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 1 * 30000;
//time to redirect, 5 milliseconds before session ends
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 1;
string str_Script = @"
var myTimeReminder, myTimeOut;
clearTimeout(myTimeReminder);
clearTimeout(myTimeOut); " +
"var sessionTimeReminder = " +
int_MilliSecondsTimeReminder.ToString() + "; " +
"var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
"function doReminder(){alert('" + msgSession + "'); }" +
"function doRedirect(){ window.location.href=window.location.href; }" + @"
myTimeReminder=setTimeout('doReminder()', sessionTimeReminder);
myTimeOut=setTimeout('doRedirect()', sessionTimeout); ";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "CheckSessionOut", str_Script, true);
}
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
this.CheckSessionTimeout();
}
}