views:

353

answers:

1

I'm working through a previous webforms application to convert it to MVC and have one big issue that I can't seem to find any good resources about. I want the ability to capture the identity of the user (windows auth set in the web.config) but in the global.asax I can't seem to get access to session (but I can get the identity information). Or when I'm working inside a base class for my controllers, I don't have access to the httpContext in the constructor (but I do have access to session)

Anyone have a good solution for this issue? Previously in webforms I had a master page that did some verification and set some session vars depending on your id/etc

+1  A: 

You can always get access to the session, or any other httpContext based entity, so long as they have already been instantiated, by using this line of code

HttpContext.Current.Session
HttpContext.Current.Request
HttpContext.Current.Server
...etc

But also, you should always have access to the User & Identity information without needing to persist it to the session unless you are making modifications to the Identity and storing those modifications separately.

HttpContext.Current.User.Identity
Goblyn27
That is what I thought, but when I view httpcontext.current.session in the context of the global.asax it's null - and when I'm inside the base controller my controllercontext (and httpcontext) is null so I can't get the identity (am I missing something ?) Do I need to do something special inside my controller to init the base correctly?
Toran Billups
Well, I have to admit that I do not know enough about the MVC framework to speak authoritatively on that. But from the Global asax you definitely should have access to those objects so long as they are already instantiated. Which events are you acting upon when you try it. For instance, application_start doesnt have a user and by default wont have a session. Session_start on the otherhand should.
Goblyn27
This is my issue, Session_Start does have access to session, but not the identity value needed. BeginRequest has access to the identity but session is null ... any method I could get access to both?
Toran Billups
The final solution I have feels very dirty ... in the authRequest method I capture the identity and set a private var - then inside the session_start I do the auth work ... man I need to put more time into this solution (thanks for the help!)
Toran Billups