【问题标题】:Where can I find user identity when using webapi with Windows Authentication on IIS8在 IIS 8 上使用带有 Windows 身份验证的 web api 时,我在哪里可以找到用户身份
【发布时间】:2015-06-08 21:11:41
【问题描述】:

我正在尝试找出如何在 webapi 中找到登录用户的身份。在开发服务器和较旧的 IIS7 上,身份在多个地方都可用,但是当我尝试在 IIS8 上设置我的应用程序时,所有已知来源都是空白的。

使用<authentication mode="Windows" />我通常可以在以下位置看到用户身份:

System.Web.HttpContext.Current.User.Identity.Name
User.Identity.Name
Thread.CurrentPrincipal.Identity.Name

但在 IIS8 上,它们都是空的。

任何想法在哪里寻找,或者如何配置 IIS8 或我的应用程序来查找用户身份?

【问题讨论】:

标签: c# asp.net-web-api windows-authentication iis-8


【解决方案1】:
ApiController.User

是你需要的。 Web API 没有 System.Web,因此它的行为不同。

【讨论】:

  • 关于如何在控制器之外的 http 模块中获取用户的任何建议?
【解决方案2】:

您正在使用匿名用户进行身份验证。
您可以通过编辑web.config 来禁止匿名访问:

<system.web>
  <authorization>
    <deny users="?" />   <!-- The question-mark represents the `anonymous`-user) -->
  </authorization>
</system.web>

通常当System.Web.HttpContext.Current.User.Identity.Name 为空时,Windows 身份验证失败。看看HttpContext.Current.Request.IsAuthenticated

比较一下 IIS7 和 IIS8 的设置
(应用程序池、web.config 以及 IIS 身份验证设置

另一个问题可能是您正在使用的客户端(浏览器及其设置)。 可能浏览器没有提供 windows 身份。

对于 Internet Explorer,请验证:
Settings -&gt; Advanced -&gt; "Enable Integrated Windows Authentication*"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2019-10-25
    • 2018-08-18
    • 1970-01-01
    相关资源
    最近更新 更多