Solved

How do I detect if a session variable has expired?

Posted on 2009-07-01
4
442 Views
Last Modified: 2012-05-07
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
Comment
Question by:cdemott33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 200 total points
ID: 24756476
Just check If Session("LoginId") is Nothing
0
 
LVL 4

Assisted Solution

by:j_s_kelley
j_s_kelley earned 200 total points
ID: 24756558
the session ID will be null (C#) or Nothing (VB) once it expires.  
0
 
LVL 2

Assisted Solution

by:nikege
nikege earned 100 total points
ID: 24757986
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
 

Author Closing Comment

by:cdemott33
ID: 31598862
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question