我需要在MVC 5中获取当前用户名

时间:2018-11-16 14:33:20

标签: asp.net-mvc

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

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

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

Server settings

我看过的每一篇帖子都建议按照已经完成的步骤进行操作,但是我仍然没有运气。

1 个答案:

答案 0 :(得分:0)

使用此方法

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

User.Identity.Name
相关问题