Search code examples
c#asp.net-mvcsession-stateglobal-asaxhttpcontext

ASP.NET MVC4 - Session_End - How can I get the currently logged on user's name in Session_End of Global.asax?


My issue is that once Session_End executes in my Global.asax, the HttpContext.Current object no longer exists- probably as expected, I would imagine. So, what I'm trying to do is once the session ends, update my Logins table for the user currently logged in and set their LoggedIn status to False. Here's my Session_End:

protected void Session_End(object sender, EventArgs e)
{
    Helpers.OperationContext.UpdateIndividualLogin();
}

As you can probably guess, I can try to pass in:

System.Web.HttpContext.Current.User.Identity.Name

But this object no longer exists because I can only imagine that it's already been disposed of. So, is there any way I can grab the currently (or previously current) user's name?


Solution

  • You can do this by storing the information you want (in this case the user name) in Session. You can store it when the user is authenticated.