Friday, 5 February 2016

Automatically redirect User to Login page after Session Timeout in ASP.Net

redirect to login page when user session expires

Description:-


In this example we explain that how to redirect to login page when user session expires in asp.net.
Or how to automatically redirect user after session timeout in asp.net. Here we set session timeout 1 minute in web.config. In every web application we need to maintain the session expiration time for the security reason.

Here in this example user is automatically redirect to the login page when session time is expired like 1 minute for this example.

Code :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Configuration;

public partial class WebApplication1_SessionTimeout : 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);
    }
}




0 comments:

Post a Comment