Sachin Kainth
Sachin Kainth

Reputation: 46740

Forms authentication and session expiry

I have a requirement where I need to store when a user logs into the site and when he logs out. This is easy. The problem is sometimes users don't log out but just close their browsers. In this case I want to store the datetime that their session expired in place of the datetime that they logged out. The question is how is this achieved? Is there some event that gets triggered or something like that that I can use to "capture" a session expiry?

Upvotes: 1

Views: 202

Answers (2)

MethodMan
MethodMan

Reputation: 18843

If you do not want to track by GUID you could do the following as well

void Session_Start(object sender, EventArgs e) 
{
   // Code that runs when a new session is started
   var sessionId = Session.SessionID();
}

Upvotes: 1

Nesim Razon
Nesim Razon

Reputation: 9794

On your global.asax file you can catch session end.

protected void Session_End(Object sender, EventArgs e)
{
    // log out user
}

If you want, you can add a custom identifer for user than check on session_end

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

Upvotes: 1

Related Questions