-
02-01-2020 - |
题
我想获取用户登录的当前的用户名。我正在使用以下代码,但它返回其凭据在IIS应用程序池标识中使用的用户。MVC网站部署在服务器上。
我使用了
string userid = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string userid = Environment.UserName;
string userid = System.DirectoryServices.AccountManagement.UserPrincipal.Current.GivenName.ToString();
string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()
但没有什么似乎给了我正确的答案。
我需要在HomeController
的构造函数中获取用户名。
下面是我的web.config
<authentication mode="Windows" />
<authorization>
<allow users="*" />
</authorization>
. 解决方案
只是
string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()
.
使用它,这将工作正常
string userid = HttpContext.Current.User.Identity.Name.ToString()
. 不隶属于 StackOverflow