我想获取用户登录的当前的用户名。我正在使用以下代码,但它返回其凭据在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() 
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top