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 J   on Jun 15, 2013   Category: ASP.NET  | Level: Intermediate  |  Views: 14798    |  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.


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 .


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



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)





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');




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.


User Responses | Post Article
Comment posted by Thamilselvan J 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)
Comment 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
Comment 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 .
Comment posted by Abhishek Gupta on 12/10/2013 12:57:07 PM | Points : 10
Thanks Thamilselvan :)
It is working .
Comment 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.
  Most viewed Articles
 Submit feedback about this article
Please sign in to post feedback