28

I need to access Session variables on Session_End event in global.asax.cs, but HttpContext.Current is null, so none of the session variables are accessible.

a) Can I access user session somehow differently, or

b) Is there any other event jut before Session_End, I could access user's session variables?

Ala
  • 1,505
  • 1
  • 20
  • 36
the berserker
  • 1,553
  • 3
  • 22
  • 39

3 Answers3

49

HttpContext.Current is not available inside of Session_End, but you can access the session instance directly with this.Session.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Ray
  • 21,485
  • 5
  • 48
  • 64
  • 2
    Yes. System.Web.SessionState.HttpSessionState is the one I should use instead of HttpContext.Current. Thanx – the berserker Jan 09 '10 at 19:00
  • really helpful after seen http://stackoverflow.com/questions/12294532/asp-net-values-of-session-variables-in-session-end-event this I doubt why it is giving null . and check that I had used HttpContext.Current (y – MSTdev May 03 '17 at 10:57
0

Unless you are using Session_End for the specific purpose of taking actions when a session times out, relying on Session_End is not a good idea.

I don't believe there is any reliable way to capture the end of a user's session (for example when they close the browser).

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
  • 2
    This is not necessarily true. If you are using InProc sessions, when a session has not had activity in [timeoutvalue] minutes, it will fire. If you are using stored sessions, this doesn't happen. – womp Jan 08 '10 at 19:35
  • 1
    i think relying on session_end would be the only way if you need an accurate global list of active user sessions – the berserker Jan 08 '10 at 21:53
0

What are you trying to do?

There is no event just preceeding Session_End which would you can use to access the Session object.

If you stored a action timestamp relating to the last time the Session object was used, and then checked that, you could get some idea of when it should end.
(Based on your IIS settings / configuration). However the act of accessing it would prolong the life of the Session object.

Of course you'd need to check this somehow, either by using a hidden Ajax postback which fires an event or something similiar.

But really I wouldn't advise this. What are you trying to do that you need this?

Bravax
  • 10,453
  • 7
  • 40
  • 68
  • 1
    I am trying to keep the global list of all users with active session. So therefore I need to clean user's session when it ends (let's consider, that user didn't log-off, he or she has just closed the browser), so I need to delete this user from global hashtable, by some session GUID. As far as I know, SessionID can be different for different requests, so I can't use that one – the berserker Jan 09 '10 at 18:29