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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
HttpContext.Current is not available inside of Session_End, but you can access the session instance directly with this.Session.
Method 2
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).
Method 3
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?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0