How do I get the current user in an MVC Application? [duplicate]

喜你入骨 提交于 2020-01-01 22:13:35

问题


Possible Duplicate:
How to get current user in 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.

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

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


回答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

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

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



来源:https://stackoverflow.com/questions/1045197/how-do-i-get-the-current-user-in-an-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!