Toran Billups
Toran Billups

Reputation: 27399

How to add User.Identity to session (or something) using MVC

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

Upvotes: 1

Views: 2329

Answers (1)

Peter Lange
Peter Lange

Reputation: 2876

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

Upvotes: 3

Related Questions