首页 > 解决方案 > 我需要在 MVC 5 中获取当前用户名

问题描述

我需要使用 HttpContext.Current.User.Identity.Name,但它总是返回为空。我在项目属性中将匿名身份验证设置为禁用,并将 Windows 身份验证设置为启用。我的 web.config 中也有以下几行。

<authentication mode="Windows" />
<authorization>
  <deny users="?"/>
</authorization>

<providers>
....
  <add name="WindowsProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>

服务器设置

我看过的每篇文章都建议做我已经做过的步骤,但我仍然没有运气。

标签: asp.net-mvc

解决方案


用这个检查一下

string userName = System.Web.HttpContext.Current.User.Identity.Name

或者

User.Identity.Name

推荐阅读