简体   繁体   English

如何使用MVC将User.Identity添加到会话(或其他内容)

[英]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. 我正在通过以前的Webforms应用程序进行工作,以将其转换为MVC,但有一个大问题,我似乎找不到任何好的资源。 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). 我希望能够捕获用户的身份(在web.config中设置的Windows身份验证),但是在global.asax中,我似乎无法访问会话(但我可以获取身份信息)。 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) 或者,当我正在为控制器的基类内部工作时,我无权访问构造函数中的httpContext(但我确实有权访问会话)

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 以前在Webforms中,我有一个母版页,该母版页进行了一些验证并根据您的id / etc设置了一些会话变量

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的实体,只要它们已被实例化即可

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. 而且,除非您正在对Identity进行修改并将这些修改单独存储,否则您应该始终可以访问User&Identity信息,而无需将其保留在会话中。

HttpContext.Current.User.Identity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM