问题
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