如何在MVC应用程序中获取当前用户? [重复]

时间:2021-11-09 07:27:34

Possible Duplicate:
How to get current user in Asp.Net MVC

可能重复:如何在Asp.Net MVC中获取当前用户

I've tried a bunch of different things, I know I've done it before when I used Webforms, but I can't seem to figure out how to do it in MVC.

我尝试了很多不同的东西,我知道我之前在使用Webforms时已经完成了,但我似乎无法弄清楚如何在MVC中做到这一点。

Membership doesn't work User gives me an IPrincipal or something User.Identity gives me the same type of variable...

成员资格不起作用用户给了我一个IPrincipal或者User.Identity给了我相同类型的变量...

Anyone know? Is there any GetCurrentUser() method or something similar?

谁知道?是否有任何GetCurrentUser()方法或类似的东西?

2 个解决方案

#1


See Mehrdad's answer here: System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

请参阅Mehrdad的答案:System.Web.UI.Page将不允许我从ASP.Net-MVC中的Controller访问CurrentUser或User.Identity

He explains that you can to use

他解释说你可以使用

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere

#2


Page.User.Identity

Page should be referencing System.Web.UI.Page

页面应该引用System.Web.UI.Page

check do you have any class named "Page" within your project

检查项目中是否有任何名为“Page”的类

#1


See Mehrdad's answer here: System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

请参阅Mehrdad的答案:System.Web.UI.Page将不允许我从ASP.Net-MVC中的Controller访问CurrentUser或User.Identity

He explains that you can to use

他解释说你可以使用

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere

#2


Page.User.Identity

Page should be referencing System.Web.UI.Page

页面应该引用System.Web.UI.Page

check do you have any class named "Page" within your project

检查项目中是否有任何名为“Page”的类