【问题标题】:HttpContext.Current.User.Identity.Name returns blankHttpContext.Current.User.Identity.Name 返回空白
【发布时间】:2021-03-08 23:44:42
【问题描述】:

我正在使用 HttpContext.Current.User.Identity.Name 在使用 Web 应用程序时获取用户名。在开发过程中,我使用的是本地 iis,启用了集成的 windows 身份验证,启用和禁用了匿名访问,并且我能够获取用户名。

现在,当我发布 Web 应用程序时,它返回空白。发布服务器上的设置是一样的,我尝试使用Page.User.Identity.Name,也返回空白。

有谁知道这是为什么以及如何解决它?

【问题讨论】:

  • 是 HttpContext.Current.User.Identity.IsAuthenticated == true?
  • 我没有使用过这个,因为在开发过程中我一直并且仍在开发中获取用户身份
  • 您使用什么类型的身份验证?是否启用匿名身份验证?
  • 使用 Windows 身份验证,我启用和禁用了匿名身份验证
  • 禁用匿名认证,在屏幕上写IsAuthenticated看是真是假。

标签: asp.net windows-identity


【解决方案1】:

您可能启用了匿名身份验证以及 Windows 身份验证。关闭匿名。

所以,

<system.web>
    <authentication mode="Windows" />
</system.web>

在应用程序的 IIS 配置中, 查看身份验证选项卡

Set **Anonymous Authentication** to **Disabled** and
Set **Windows Authentication** to **Enabled**

这应该可以工作,并且 Identity.UserName 现在应该正确显示。

【讨论】:

  • 我最近遇到了同样的问题,这个解决方案不起作用。您还能想到其他原因吗?
【解决方案2】:

HttpContext.Current.Request.LogonUserIdentity.Name 在 VS 2012 环境和 IIS 7 中始终为我工作

【讨论】:

    【解决方案3】:

    要解决此问题,您必须启用 Windows 身份验证功能。请按照以下步骤操作:

    -单击开始,然后单击控制面板。打开“程序”组。
    - 在程序和 - 功能下,单击打开或关闭 Windows 功能。
    - 展开标有 Internet 信息服务的项目。
    - 展开标有万维网服务的项目。 -扩展项目安全性->
    确保选择 Windows 身份验证

    您还需要从 IIS 禁用匿名身份验证,如下所示: - 在 IIS 中单击您的应用程序 - 双击 IIS 组下的身份验证 -单击匿名身份验证 - 单击操作下右侧的禁用。 希望这会有所帮助

    【讨论】:

      【解决方案4】:

      使用 WIF 时,您应该使用 Thread.CurrentPrincipal.Identity.Name 而不是 User.Identity.Name。

      在此处阅读更多信息:http://msdn.microsoft.com/en-us/magazine/ff872350.aspx 以了解有关 Windows Identity Foundation 的更多信息

      类似问题:User.Identity.Name is null after authenticate via WIF

      【讨论】:

        【解决方案5】:

        在 web.config 文件中设置 &lt;authentication mode="Forms"&gt;,您的问题将得到解决。

        使用以下代码测试您的网站

        if (Page.User.Identity.Name != "" ) { Label1.Text = "Hello"; } else { Response.Redirect("login.aspx?url=Upload.aspx"); }

        【讨论】:

          【解决方案6】:

          这不会解决原始帖子,但无论如何都想把它放在这里,以防其他人在搜索为什么 user.identity 什么都不返回时偶然发现这个......

          在我的情况下,User.Identity 在更新用户广告用户名(特别是 Windows 2000 之前的用户名)后开始不返回任何内容。

          IIS 上的 LSA 缓存是问题所在。即使在对问题进行故障排除时重新启动 IIS 服务器后,问题仍然存在。直到添加此处概述的注册表设置,问题才得到解决:

          https://support.microsoft.com/en-us/help/946358/the-lsalookupsids-function-may-return-the-old-user-name-instead-of-the

          【讨论】:

          • 请将链接中的重要细节添加到您的帖子中。第三方资源的链接只能用作参考。如果链接页面不再存在,它将使此答案无用。
          【解决方案7】:

          对于空白返回,我的解决方案最终是 web.config。我使用的是 Visual Studio 2010,默认的 web.config 不起作用。我用一个几乎空的 web.config 替换它,然后成功了!可能是默认的 vs2010 web.config 调用了太多的引用,或者错误地配置了 IIS 以使用 User.Identity.Name。我的系统是Windows7。

          vs2010 的默认 asp.net 网站 web.config 大约有 100-115 行长。正如您在下面看到的,几乎为空的 web.config 大约有 20 行长。


          我使用的 web.config:

          <?xml version="1.0" encoding="UTF-8"?>
          <configuration>
          
            <system.web>
             <authentication mode="Windows" />
             <authorization>
                <allow roles="Doman Name\Group Name" users="" />
                <deny users="*" />
             </authorization>
          
            </system.web>
          
            <system.webServer>
              <modules runAllManagedModulesForAllRequests="true" />
                  <security>
                      <authorization>
                          <add accessType="Allow" users="" roles="Doman Name\Group Name" />
                      </authorization>
                  </security>
            </system.webServer>
          
          </configuration>
          

          【讨论】:

            【解决方案8】:
            1. 在 IIS 中:单击您的站点
            2. 在主页中:身份验证
            3. 在操作菜单中:打开功能
            4. 禁用匿名身份验证
            5. 重启网站

            steps 1,2,3

            step 4

            【讨论】:

            • 如果可能,请始终尝试提供此类答案的屏幕截图。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-12-23
            • 1970-01-01
            • 2017-11-20
            • 1970-01-01
            • 2011-05-27
            • 2012-09-06
            相关资源
            最近更新 更多