Click here to Skip to main content
Click here to Skip to main content

Detecting Session Timeout and Redirect to HomePage

, 13 Sep 2006
Rate this:
Please Sign up or sign in to vote.
Application that Detects when a Session has timed and redirect user to Home Page
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

This application Detects when a Session has timmed and redirect user to the home page. Session Timeout can be changed in the Web.config file. Best to set this value to 1 (<sessionState ....timeout="1") to see quick effect. Unzip and Place projcet in the C:\Inetpub\wwwroot. Run Applcation and and click on the "MakeID" button followed by the "Page1" button. If session has not timed out Page1 should be displayed (click on the "Get ID" button to display the Session ID). If session has timed out a page saying "Session Time out" will be displayed (If user waites more than a minute)

Code: HomePage.aspx

The Code below created a session id when user click on the "Make ID" button.

    Private Sub bntMakeID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntMakeID.Click
        Dim session_id As Guid = Guid.NewGuid()
        Session(STATE_SESSIONID) = session_id.ToString
    End Sub
The Code below then calls "Page1.aspx" that will use the Session ID created in the code above. This code gets called when the "Page1" button is clicked
    Private Sub bntPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntPage1.Click
        Response.Redirect("Page1.aspx")
    End Sub

Code: Page1.aspx

There are two ways for setting up this code to detect session time out

Step1: Inheriting from a Base Page

Base Calss is responsible for redirecting to Home Page

PageBase.aspx

The LoadBase function is called from the Page_Init
Public Class PageBase
    Inherits System.Web.UI.Page
..
..
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
        LoadBase()
    End Sub
..
..

    Private Sub LoadBase()
        If Session("sessionID") = Nothing Then
            Response.Redirect("login.aspx")
        End If
    End Sub

End Class

Page1.aspx

Public Class Page1
     Inherits PageBase 

Step2: calling a local method

Page1.aspx calling method TestSessionStat

Page1.aspx

Note Page1 is no longer Inheriting from PageBase
Public Class Page1
    ' Inherits PageBase 
    Inherits System.Web.UI.Page

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
...
...
        TestSessionStat()
    End Sub

    Private Sub TestSessionStat()
        If Session("sessionID") Is Nothing Then 'sessionID
            Response.Redirect("login.aspx")
        End If
    End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

ONeil Tomlinson
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralTwo Methods, not Two steps! Pinmemberganeshmuthuvelu10-May-07 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 13 Sep 2006
Article Copyright 2006 by ONeil Tomlinson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid