jao
jao

Reputation: 18620

Is there a way to find out the age of an asp.net session?

How do I find out how old an asp.net (3.5) session is?

Upvotes: 5

Views: 965

Answers (1)

Rune Grimstad
Rune Grimstad

Reputation: 36330

Not directly I think, but it would be easy to do it yourself. In global.asax you can add code to the Session_Start even handler where you add a session variable that tells when the session was created.

Something like this:

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    Session["SessionStartTime"] = DateTime.Now;        
}

Then you can check how long the session has existed by doing the following in your code:

TimeSpan sessionLifeTime = DateTime.Now - (DateTime)Session["SessionStartTime"];

Upvotes: 6

Related Questions