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

时间:2009-06-25 17:27:52

标签: asp.net-mvc entity-framework membership

  

可能重复:
  How to get current user in Asp.Net MVC

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

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

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

2 个答案:

答案 0 :(得分:10)

请参阅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

答案 1 :(得分:0)

Page.User.Identity

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

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

相关问题