views:

21

answers:

1

Hi, 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.

+1  A: 

If you want to take this approach you should check httpcontext.current.session, but of course first make sure it's not null. In addition, you'll want to check to see that the Request IsAuthenticated as well to ensure there is a user because you can have a session that isn't authenticated.

Nissan Fan