首页 > 解决方案 > User.Identity.Name 给出 IIS 服务器用户名而不是客户端用户名

问题描述

在我的 MVC 应用程序中,我试图在右上角显示域/用户,它在带有 User.Identity.Name / Request.LogonUserIdentity.Name 的 Visual Studio 上运行良好

当我在服务器上托管它时,它显示 server_domain/server_username。

我已经将身份验证模式 = windows 并模拟为 true。使用 ApplicationPoolIdentity 和 LoadUserProfile = true。

我知道我需要关闭匿名并打开 Windows 身份。我已经做到了。我还编辑了我的 applicationHost.config 并使覆盖允许身份验证模式。我已经在同一个文件中删除了两者的锁定。

我的本地域和服务器域是不同的。

我不明白我错过了什么/做错了什么。有人可以帮我吗?

标签: asp.net-mvcvisual-studioiismodel-view-controllerwindows-authentication

解决方案


您可以使用HttpContext.User.Identity.Name获取客户端用户名。

另外,请确保您在 web.config 中的 system.web 中有授权标签

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

https://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/


推荐阅读