Solved

How to check session is expired or not--possible approach

Posted on 2012-03-26
5
315 Views
Last Modified: 2012-04-25
I found the below Info at http://forums.asp.net/t/1138444.aspx

My question: "What would be the best place to initialize Session["UserId"]=User.Identity.Name; ??  "

In Session_Start(..) of Global.asax ?? Or Another Location?

The info:

Usually when its user/role based application we always store userid/username in session.

So this is a variable that an application always needs.... So you can even check the individual variable which is maitained for Session Tracking/Identification.

if (Session["UserId"] == null)
{

    Response.Redirect("Login.aspx?reason=SessionTimeout"); //Passing the reason in querystring to notify the user in Login page.
}
0
Comment
Question by:niceguy971
5 Comments
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 250 total points
Comment Utility
Have a look here:

http://forums.asp.net/t/1283071.aspx/1

and

http://forums.asp.net/t/1667680.aspx/1

It actually does not matter where you start your session. Although the best place is Session_Start

Hope this helps!
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 250 total points
Comment Utility
i think the best place would be when your authentication of login is complete.
Say you have Authenticate method in Login.aspx page handler.

once the use if authenticated, you can set the session there like this:-

private void Authenticate(string userid,string password)
{
if(DoAuthenticate(userid,password))
{
Session["UserId"]=userid;
}
}

private bool DoAuthenticate(string userid, string password)
{
//Authenticate here by matching the userid and password
}

Open in new window

0
 
LVL 9

Expert Comment

by:gery128
Comment Utility
@niceguy971
Why you want to initialize Session["UserId"] ?
You already have that in User.Identity.Name which is available throughout the application. So you can access it anywhere you want in application without transferring to Session object as an additional variable.
0
 

Author Comment

by:niceguy971
Comment Utility
Hi gery128. My original question : "How to check if session is expired?"  Per Info at

http://forums.asp.net/t/1138444.aspx it's possible to use Session["UserId"] ..or another

Session["something"]..so i was asking "What would be the best place to initialize

Session["UserId"] or Session["something"] ?"

Did you read the info at http://forums.asp.net/t/1138444.aspx ?
0
 

Author Closing Comment

by:niceguy971
Comment Utility
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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