Server Intellect
 
Home > Articles > ASP.NET Articles > How to do auto logout and redirect to login page when session expires using asp.net?

How to do auto logout and redirect to login page when session expires using asp.net?

by Thamilselvan Jagadeesan   on Jun 15, 2013   Category: ASP.NET  | Level: Intermediate  |  Views: 24156    |  Points: 100   
Like this article? Bookmark and Share:
In this article I will explain you how to auto logout and redirect to login page when users session expires within specific period.




Introduction


In every applications, we need to maintain the session expiration time say for example 1hr or 30 minuets and this configuration item we can do with web.config file or in IIS. But here, application should redirect to login page automatically when session expires.


Configure session time


First configure the session timeout value in web.config file as like below, here I’m configuring the session timeout value as 3 minutes for this sample .

<system.web>

<sessionState mode="InProc" timeout="3"></sessionState>

</system.web>

 

Create Pagebase class


create a custom pagebase class and write the common functionality codes into this class. Through this class, we can share the common functions to other web pages. In this class we need inherit the System.Web.UI.Page class. Place the below code into Pagebase class.e

public class PageBase : System.Web.UI.Page

{

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender(e);

AutoRedirect();

}

public void AutoRedirect()

{

int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000);

string str_Script = @"

<script type='text/javascript'>

intervalset = window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + @");

function Redirect()

{

alert('Your session has been expired and system redirects to login page now.!\n\n');

window.location.href='/login.aspx';

}

</script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script);

}

}

 


Above AutoRedirect function will be used to redirect the login page when session expires, by using javascript window.setInterval, This window.setInterval executes a javascript function repeatedly with specific time delay. Here we are configuring the time delay as session timeout value. Once it’s reached the session expiration time then automatically executes the Redirect function and control transfer to login page.

In testAutologout page, we need to inherit the PageBaseclass as like below also same thing we need to inherit wherever required this functionality.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace TestWeb

{

public partial class testAutoLogout : PageBase

{

protected void Page_Load(object sender, EventArgs e)

{

}

}

}
 


Once application reached session expiration time if suppose there is no action performed by user, it shows the warning message as like below and if clicks the Ok in the message box it redirects to login.aspx page.


This is the one of way to redirect to login page if session expires. Hope this sample will help you and provide your feedback and suggestions on this.

 

Like this article? Bookmark and Share:

Most viewed Articles

User Comments


  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Abhishek Gupta
on 11/14/2013 12:42:07 PM
Points : 10

Do I need to call this Autoredirect Funtion in every page.
because the above code is not working for me .
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Thamilselvan Jagadeesan
on 11/15/2013 1:29:11 AM
Points : 10

Hi Abhishek Gupta,

Create the custom PageBase class and place the above AutoRedirect() method as shown and inherit this PageBase class in every page as like below
public partial class testAutoLogout : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Abhishek Gupta
on 12/10/2013 12:57:07 PM
Points : 10

Thanks Thamilselvan :)
It is working .
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by jayanti roy
on 1/29/2014 1:00:00 AM
Points : 10

Hello ,
I have used session time out =10 minutes,but when i had used ur code in my master page through which all pages are getting inherited even login page also ,it is showing popup message in each seconds,again and again getting message .Please giev me ur valuable suggestions


Thanks in advance
Jayanti
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Sits
on 3/13/2014 1:59:22 PM
Points : 10

Great article, i was able to get it working in c# easily. I hate to ask but how about a vb version? Not my preference but I tried to convert and found too many errors.
Thanks!
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Santhosh Kumar
on 5/30/2014 7:55:25 PM
Points : 10

Hi Thamilselvan J ,
Will this script consider the sliding expiry of session timeout ? . Something like autoextending . The timeout value will not extend with user activity right ?
Santhosh
  Re :How to do auto logout and redirect to login page when session expires using asp.net?   
Posted by Will Dunne
on 7/3/2014 3:12:33 AM
Points : 10

@ Sits

I have tried to implement it using VB, it does render the script but doesn't appear to be firing the event. Here's my VB coded version:
BasePage
Imports Microsoft.VisualBasic
Imports System.Web.UI.Page

Public Class BasePage
Inherits Page

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
AutoRedirect()

End Sub
Private Sub AutoRedirect()

Dim int_MilliSecondsTimeOut As Int32 = (Session.Timeout * 60000)

Dim str_Script As String = " var intervalset = 'window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + ")" & vbCr + "function Redirect() {" & vbCr + " alert('Your session has been expired and system redirects to login page now.!\n\n'); " & vbCr + "window.location.href='/manualLogon.aspx'; " & vbCr + "}" & vbCrLf

Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "Redirect", str_Script, True)
End Sub

End Class


Default.aspx.vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits BasePage


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class


JavaScript code as it appears in the page
<script type="text/javascript">
//<![CDATA[
var intervalset = 'window.setInterval('Redirect()',180000)
function Redirect() {
alert('Your session has been expired and system redirects to login page now.!\n\n');
window.location.href='/manualLogon.aspx';
}
//]]>
</script>


If anyone can help workout why the event isn't firing please feel free to comment.

Submit feedback about this article

Please sign in to post feedback