Related Posts Plugin for WordPress, Blogger...

How To Add Session Timeout Warning in ASP.Net


How To Add Session Timeout Warning in ASP.Net
The following steps will display a JavaScript alert that the session will expire two minutes before the session timeout.

1. Create a class basePageSessionExpire.vb

Imports Microsoft.VisualBasic

Public Class basePageSessionExpire
Inherits System.Web.UI.Page

Public Sub basePageSessionExpire()
End Sub


Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)

If Context.Session IsNot Nothing Then
If Session.IsNewSession Then
Dim szCookieHeader As String = Request.Headers("Cookie")
If (szCookieHeader IsNot Nothing) AndAlso (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) Then
Dim strScript As String = "<script language='javascript'> alert('Session has expired.'); window.close(); </script>"
ClientScript.RegisterStartupScript(GetType(String), "alert", strScript)
End If
End If
End If
End Sub

End Class

2. Add EnableSessionState="True" in every Page Directive.

3. Change all Inherits System.Web.UI.Page to Inherits basePageSessionExpire

4. In your web.config, add <sessionState mode="InProc" cookieless="false" timeout="20"/> before the </system.web>.

5. Create a hidden link in your Master page with this code inside:
Protected Sub lnkContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContinue.Click

HiddenField1.Value = 1
End Sub

6. Add this code at your Master Page's aspx:

<script language="javascript" type="text/javascript">
var timerPROMPT;
var timeoutval;

timeoutval = document.getElementById('<%=hdnTimeout.ClientID %>').value;

function TimeOutAlert()
{

if(confirm('Your session will be terminated soon. Click OK to stay in this session or Cancel to exit'))
{

document.getElementById('<%=lnkContinue.ClientID %>').click();
timerPROMPT = window.setTimeout("TimeOutAlert()",timeoutval);

}
else
{
window.close();
}

}
timerPROMPT = window.setTimeout("TimeOutAlert()",timeoutval);
</script>

If you like this post, buy me a cup of coffee.

Share This Post Digg This Del.icio.us Furl Reddit Fav This With Technorati Yahoo MyWeb Stumble This

0 comments


Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting