0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

ContextOnBeginRequest is your BeginRequest event handler. If this event

fires the SessionState is not createt yet.

s. HttpApplication Class -> Remarks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.