Search code examples
asp.nethttpmodule

Save username in session variable when users logins using Http Module


Is it possible to use an application event to save the username in a session variable? I would like to do something like this:

        private void ContextOnBeginRequest(object sender, EventArgs eventArgs){
        if (_context.Request.IsAuthenticated)
            _context.Session["ID"] = _context.User.Identity.Name;
    }

However in the above code I get an error saying that Session state is not available.


Solution

  • ContextOnBeginRequest is your BeginRequest event handler. If this event

    fires the SessionState is not createt yet.

    s. HttpApplication Class -> Remarks