Reputation: 12241
During my authorization processes, I need to set some data into the session so that it can travel with every request made by the web page. As soon as the authorization is approved in my AuthorizationAttribute.AuthorizeCore(), I make some calls to our services to retrieve the necessary data that needs to travel with every request and stuff it into the session via httpContext.Session["MyInfoObject"] = myInfoObject
and then return true.
The session cookie is set and the next request shows that the user is now authenticated. However, when I attempt to retrieve the data that was previously stored via var myInfoObject = requestContext.HttpContext.Session["MyInfoObject"] as MyInfoObject;
the entire session value collection is empty. I observed the Session object and I could see that the Session object is populated, and the SessionID is the same as the one in the session cookie in the browser, but the value collection is empty. Nothing was saved.
Any ideas?
Update I was checking the session values in the controller base class. I created a custom base class for my controllers and in the Initialize() override, I call base.Initialize() first and then start fiddling with the Session. At this stage, the session values are empty. However, if I place a break point inside of an Action on the Derived controller, my values are there. I think I'm missing something in the way the pipeline executes. Any info would help.
Upvotes: 0
Views: 2677
Reputation: 12241
I was able to solve the issue and discover a few points. What it really came down to was the state of the session object at each stage of the pipeline. It was never NULL
at any point; the problem was that all of the values I had previously stuffed into it weren't available yet based on where i was in the chain of execution. I didn't find any official documentation regarding the life cycle, but I was able to discover a few things:
Upvotes: 1
Reputation: 2513
I usually access the Session variables in a base controller by overriding the OnActionExecuting method. I think initialize is too early in the process. See enter link description here
Upvotes: 0