Solved

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

Posted on 2012-03-26
5
321 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
[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
5 Comments
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 250 total points
ID: 37769734
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
ID: 37769801
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
ID: 37769879
@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
ID: 37775063
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
ID: 37895644
Thanks
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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