在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
当前回答
在Asp.net Mvc Identity 2中,你可以通过以下方式获取当前用户名:
var username = System.Web.HttpContext.Current.User.Identity.Name;
其他回答
您可以在ASP中获取用户名。NET MVC4:
System.Web.HttpContext.Current.User.Identity.Name
不管怎样,在ASP中。NET MVC 3你可以使用User来返回当前请求的用户。
使用System.Security.Principal.WindowsIdentity.GetCurrent () . name。
这将获取当前登录的Windows用户。
用户名:System.Web.HttpContext.Current.User.Identity.Name
在IIS管理器的“身份验证”下,禁用: 1)匿名认证 2)表单认证
然后添加以下到你的控制器,以处理测试和服务器部署:
string sUserName = null;
string url = Request.Url.ToString();
if (url.Contains("localhost"))
sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
else
sUserName = User.Identity.Name;