How do I detect if a session variable has expired?

Hi Experts -  I have a session variable that holds the users login id.  What I like to do is, when a new aspx page loads I want to check to makes sure this session variable still holds a value.  If it doesn't I'd like them to be redirected to the login page.

How do I do this?  Is there a way to do this in the web.config or do I need to include a snippet of code on each one of my pages.

Thanks!
<!-- THIS IS MY SESSION VARIABLE -->
 
Session("LoginID")

Open in new window

cdemott33Asked:
Who is Participating?
 
mohan_sekarConnect With a Mentor Commented:
Just check If Session("LoginId") is Nothing
0
 
j_s_kelleyConnect With a Mentor Commented:
the session ID will be null (C#) or Nothing (VB) once it expires.  
0
 
nikegeConnect With a Mentor Commented:
Add Global.asax file to your project. You will find several event handlers there. One of them will be Application_BeginRequest. It is fired each time application got a request. Use following code for this handler:
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if(Session["LoginID"] == null) // or if(Session.IsNewSession)
                Response.Redirect("Login.aspx");
        }

Open in new window

0
 
cdemott33Author Commented:
Thank you all for your answers.  I decided to go with a custom page load class checking to see if the session was "nothing".
0
All Courses

From novice to tech pro — start learning today.