Hi
in my web app users login to website normally and they have tagged in user table in database as online user in Session_End i want to log them out
In Session_End i have no access to HttpContext.Current.Session or Session only
they are both null how can i access to session variable at (Session_End) event.

- 1,662
- 6
- 28
- 49
2 Answers
If you're using InProc Session (so the Session_End event fires) you can use
this.Session
When you're using the global.Asax, Global.asax itself extends HttpApplication
From MSDN (http://msdn.microsoft.com/pt-br/library/system.web.httpapplication.aspx):
"This class is the base class for applications that are defined by the user in the Global.asax file."
I created a method inside Global.asax that returns the Session:
public HttpSessionState GetSession()
{
//Check if current context exists
if (HttpContext.Current != null)
{
return HttpContext.Current.Session;
}
else
{
return this.Session;
}
}
So I can use the following method @global.asax:
var x = GetSession()["key"];
Edit
However, multiple application instances can be generated to handle requests (you can set how many worker processes you want in
IIS > App Pools > Properties
But with different Worker Process the request can fall on one of many worker processes that won't have the session information that you want
See ASP.NET session state and multiple worker processes
I would suggest storing the first access abd the last registered access (Post_Authenticate event).
In addition use a SQL Server job running periodically to check the last access. This way you can prevent your app crashing (when the Session_End event wouldn't fire).

- 1
- 1

- 453
- 4
- 11
Session_End only works if you're using InProc session management. Maybe you're using StateServer or SqlServer approach?

- 1,594
- 1
- 15
- 20