在表单模型中,我曾经通过以下方式获取当前登录用户:

Page.CurrentUser

如何在ASP中获得控制器类中的当前用户。净MVC吗?


当前回答

HttpContext.Current.User试试。

公共共享财产当前()作为 System.Web.HttpContext System.Web.HttpContext的成员 简介: 获取或设置当前HTTP请求的System.Web.HttpContext对象。 返回值: 当前的System.Web.HttpContext HTTP请求

其他回答

我知道这真的很旧,但我刚刚开始使用ASP。NET MVC,所以我想我要坚持我的两分:

请求。IsAuthenticated告诉您用户是否通过了身份验证。 identity为您提供登录用户的标识。

不管怎样,在ASP中。NET MVC 3你可以使用User来返回当前请求的用户。

IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
        ...
                   currentUser.IsInRole("Operator");

你可以使用以下代码:

Request.LogonUserIdentity.Name;

您可以在ASP中获取用户名。NET MVC4:

System.Web.HttpContext.Current.User.Identity.Name