HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE


QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

How to detect session timeout and redirect to login page or home page
In asp.net, It is very simple to detect session time out and redirect the user to login page or home page.All you have to do is, specify the redirection page in session_start event handler in Global.asax file as shown below.

void Session_Start(object sender, EventArgs e)

{

    Response.Redirect("LoginPage.aspx");

}

If the session has timed out, the user will be redirected to the login page.Let me explain how we do this with a simple example. Our example application should have the following files:
  • Login Page
  • Home Page
  • Web.config
  • Global.asax
  1. Design the Login Page as shown in the Diagram below.
  2. The code snippet below shows the HTML for the Login Page.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginPage.aspx.cs" Inherits="LoginPage" %>

    <head runat="server">

        <title>Login Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

            <table>

                <tr>

                    <td style="width: 100px">

                        User Name</td>

                    <td style="width: 100px">

                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

                </tr>

                <tr>

                    <td style="width: 100px">

                        Password</td>

                    <td style="width: 100px">

                        <asp:TextBox ID="TextBox2" TextMode="Password" runat="server"></asp:TextBox></td>

                </tr>

                <tr>

                    <td style="width: 100px; text-align: center;" colspan="2">

                        <asp:Button ID="LoginButton" runat="server" Text="Login" OnClick="LoginButton_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="width: 100px; text-align: center">

                        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label></td>

                </tr>

            </table>

        </form>

    </body>

    </html>
  3. The code for Login Button Click is shown below.If the user has enetered the correct user name and password, the user name is stored in the Session variable and the user is redirected to the Home Page.

    protected void LoginButton_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text == "prasad" && TextBox2.Text == "prasad")

        {

            Session["UserName"] = TextBox1.Text;

            Response.Redirect("HomePage.aspx");

        }

        else

        {

            Label1.Text = "Invalid User Name / Password";

        }

    }
  4. Have the following HTML for the Home Page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="HomePage.aspx.cs" Inherits="HomePage" %>

    <head runat="server">

        <title>Home Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

        <h1>You are logged in and on the Home Page</h1>

            <p>

                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />&nbsp;</p>

        </div>

        </form>

    </body>

    </html>

  5. Have the following Code in the Code Behind file of the Home Page

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write("Hello!!!");

    }
  6. In the web.config file set the sessionState mode to InProc and timeout to 1 minute. Sample code is shown below.

    <sessionState mode="InProc" timeout="1">

  7. Include the following code in Global.asax file. Session_Start is the event handler that will be executed when ever a new session is started. When you access the Login Page a new session is started. You then enter the user name and the password and press the Login button. User Name is stored in the session variable and you are redirected to the home page. If you donot do anything on the home page for 1 minute your session will time out. After 1 minute if you press the Button on the home page you will be automatically redirected to the Login page. This is bcos we have set the session time out for 1 minute in the web.config file.

    void Session_Start(object sender, EventArgs e)

    {

        Response.Redirect("LoginPage.aspx");

    }


Your Questions/Comments/Feedback:
If you have some questions that needs an answer you can post them here. If you find this page useful please post your comments and feedback.
Title/Question:
Name:
Email:
Comments: