Solved

How do I detect if a session variable has expired?

Posted on 2009-07-01
4
440 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

752 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