获取登录用户ASP.NET MVC的用户名

我想获取登录用户名和域的用户名。 我得到它的代码是在一个控制器中:

User.Identity.GetUserId()

在我的web.config中是:

<system.web>
    <identity impersonate="false"/>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime />
    <pages controlRenderingCompatibilityVersion="4.0" />
</system.web>

目前,我只收到一个空字符串。 我必须更改以获取Windows用户名信息?

其他人的旁注 :在我的研究中,我还得到了以下几点:

Environment.UserDomainName +  @"" + Environment.UserName

就此而言,它只传递线程正在运行的身份,而不是窗口信息。

编辑1:我目前正在测试我的程序在调试模式。


尝试这个

HttpContext.User.Identity.Name

还要确保你的system.web中的web.config中有授权标签

<authorization>
  <allow users="?" />
</authorization>

并在IIS中确保您将禁用匿名身份验证并为相同的应用启用Windows

在这里输入图像描述


User.Identity.GetUserId()Microsoft.AspNet.Identity的扩展方法。 身份与Windows身份验证不兼容(您必须使用一个或另一个),所以我不确定您在这里实际做了什么。

一般来说,在Windows身份验证中, User.Identity.Name的值将采用{DOMAIN}{USER} 。 所以,如果你想要域名和/或用户名,你可以将该字符串拆分为并取出你之后的部分。

当涉及到Identity时, User.Identity.Name应该是ApplicationUser.UserName的值,但根据设置,它可能是ApplicationUser.Email

链接地址: http://www.djcxy.com/p/42037.html

上一篇: Get username of logon user ASP.NET MVC

下一篇: Host Angular2 app with routing in Azure