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

Page.CurrentUser

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


当前回答

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

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

其他回答

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

你只需要User.Identity.Name。

我们可以使用下面的代码在ASP中获取当前登录的用户。Net MVC:

var user= System.Web.HttpContext.Current.User.Identity.GetUserName();

var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; //will give 'Domain//UserName'

Environment.UserName - Will Display format : 'Username'

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

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

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

如果需要从控制器内部获取用户,请使用controller的user属性。如果你从视图中需要它,我会在ViewData中填充你特别需要的东西,或者你可以只调用User,因为我认为它是ViewPage的一个属性。