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.


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just check If Session("LoginId") is Nothing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
the session ID will be null (C#) or Nothing (VB) once it expires.  
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)

Open in new window

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".
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.