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

Detecting Session Timeout and Redirect to HomePage

By , 13 Sep 2006
 
<!-- 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

About the Author

ONeil Tomlinson
Web Developer
United Kingdom United Kingdom
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generallogin.aspxmemberAjay Kale New27-Sep-10 0:14 
Hi,
 
in my application - when I traverse for sometime to various tabs - suddenly login,aspx page is displayed.
When I checked logs - but no debugger statement printed there..and no javascript alerts are seen.
 
So I verified web.config for timeout properties but couldnot find anything to be changed
 
<authentication mode="Forms">
<forms loginUrl="login.aspx" protection="All" timeout="600" slidingExpiration="true" >
</forms>
</authentication>
 
and
 

<sessionState mode="InProc" timeout="600" />
 
Thanks and regards,
- Ajay Kale
Generaljquery ui layout pane - pane resize when window resizemembernur896-Jun-10 21:32 
Hi there,
 
I'm new in jquery....
for my website, i'm used west and center pane instead of iframe.
i had problem to set the default size for both pane...
1. i want set the west pane,70% and the center pane,30%..
2. when user resize the browser window, the west and center pane should be resize to..on this problem...i really don't know how and what to do...
 
please help me...
i'm using jquery.ui.all.js and jquery.layout.js
 
<script type="text/javascript">
 
var myLayout; // a var is required because this page utilizes: myLayout.allowOverflow() method
var westLayout,centerLayout;
$(window).ready(function () {
myLayout = $('body').layout({
west__size:"700" //i put this size to make sure that the west pane greater than center pane.
, center__size:"300"
});
});
</script>
 
thanks...
Nur

QuestionCan I get C# version of this sample codememberRakesh Bhavsar6-Jan-10 18:29 
Can I get C# version of this sample code
GeneralTwo Methods, not Two steps!memberganeshmuthuvelu10-May-07 13:09 
instead of labeling Step 1 and Step 2, it should be labeled as Method 1 and Method 2
Questionset session time outmemberrompi9-May-07 19:55 
how we set time out for a page

 
Signature preview shobha 10:49 20 Apr '07
[Message text goes here]

AnswerRe: set session time outmemberONeil Tomlinson9-May-07 22:19 
you can set it in your web config for asp.net application or you can sent the timeout period for the session object.
QuestionWhat about the first visit?memberrobrich13-Sep-06 10:16 
This solution requires them to push a button to initiate. That's fine, except for "remember me" cookie type login scenarios. In that case, they get to the first page without ever pushing the button. We could run this process anyway, but then anyone going to the site would get to index.aspx from any link. For example, they're given this link: "http://www.mysite.com/mypage.aspx?myarg=myvalue". They click on it. The page detects that Session("sessionId") is nothing, and sends them to index.aspx -- hardly what they had in mind.
AnswerRe: What about the first visit?memberONeil Tomlinson13-Sep-06 22:32 
Well in a situation like that you would redirect them to a "login" page or a "Session Failed" page
GeneralRe: What about the first visit?memberrobrich14-Sep-06 5:11 
But technically, they're still valid. Their cookie says the login is ok.
GeneralRe: What about the first visit?memberSam.M8-Aug-07 22:07 
So, What u can do is: first check the Session, if it's expired then go to cookie and check whether its there or nor. If it's there then take that otherwise redirect to login.
 
Regards n Thks
 
Sam.M

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

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