如何在MVC应用程序中获取当前用户?

Mat*_*att 0 membership asp.net-mvc entity-framework

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

我已经尝试了一堆不同的东西,我知道我当我用Web表单之前已经做了,但我似乎无法弄清楚如何做到这一点的MVC.

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

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

Mat*_*att 10

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

他解释说你可以使用

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere
Run Code Online (Sandbox Code Playgroud)