可能重复:
如何在 Asp.Net MVC 中获取当前用户
我尝试了很多不同的事情,我知道我以前在使用 Webforms 时做过,但我似乎无法弄清楚如何在 MVC 中做到这一点。
会员资格不起作用用户给了我一个 IPrincipal 或其他东西 User.Identity 给了我相同类型的变量......
有人知道吗?是否有任何 GetCurrentUser() 方法或类似的方法?
可能重复:
如何在 Asp.Net MVC 中获取当前用户
我尝试了很多不同的事情,我知道我以前在使用 Webforms 时做过,但我似乎无法弄清楚如何在 MVC 中做到这一点。
会员资格不起作用用户给了我一个 IPrincipal 或其他东西 User.Identity 给了我相同类型的变量......
有人知道吗?是否有任何 GetCurrentUser() 方法或类似的方法?
在此处查看 Mehrdad 的答案:System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC
他解释说你可以使用
User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere
Page.User.Identity
页面应该引用System.Web.UI.Page
检查您的项目中是否有任何名为“Page”的类