Solved

How do I detect if a session variable has expired?

Posted on 2009-07-01
4
436 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
Comment Utility
Just check If Session("LoginId") is Nothing
0
 
LVL 4

Assisted Solution

by:j_s_kelley
j_s_kelley earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now