Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

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

0
cdemott33
Asked:
cdemott33
3 Solutions
 
mohan_sekarCommented:
Just check If Session("LoginId") is Nothing
0
 
j_s_kelleyCommented:
the session ID will be null (C#) or Nothing (VB) once it expires.  
0
 
nikegeCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now