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

Page.CurrentUser

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


当前回答

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

其他回答

这个页面可能就是你要找的: 在MVC3中使用Page.User.Identity.Name

你只需要User.Identity.Name。

用户名:

User.Identity.Name

但如果你只需要获取ID,你可以使用:

using Microsoft.AspNet.Identity;

所以,你可以直接获得用户ID:

User.Identity.GetUserId();

用户名:System.Web.HttpContext.Current.User.Identity.Name

使用System.Security.Principal.WindowsIdentity.GetCurrent () . name。

这将获取当前登录的Windows用户。

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

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