Solved

How do I detect if a session variable has expired?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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