在Web API中正确执行“ Windows”身份验证?

use*_*635 7 .net c# windows-authentication asp.net-web-api asp.net-web-api2

我创建了一个Web Api 2应用程序,该应用程序仅在公司网络上使用。我已经阅读了有关Web API中Windows身份验证的信息,因此这似乎是有可能的。但是我需要找出正确的实现方法。我在Web.config中包含了以下xml:

<system.web>
  <authentication mode="Windows" />   
</system.web>
Run Code Online (Sandbox Code Playgroud)

我似乎还记得旧式Webforms应用程序中的某种类型的事件挂钩。像BeginRequest()这样的东西,可以在呈现页面之前进行安全检查。我在我的一个控制器方法中将以下代码行作为第一行,但返回的值似乎只是一个空对象,没有任何有意义的信息:

var identity = HttpContext.Current.User.Identity as WindowsIdentity;
Run Code Online (Sandbox Code Playgroud)

Web API 2是否支持Windows身份验证?我错过了一步吗?如果我提交了Postman的一般测试要求,Windows身份验证应该可以工作吗?我也尝试了这段代码,但是得到了一个类似的空对象:

var x = RequestContext.Principal;
Run Code Online (Sandbox Code Playgroud)

我隐约记得一个IIS设置,例如“启用集成安全性”。您能否指定确切的设置?如果我在IIS Express上运行应用程序,是否可以完成此任务?

更新

我遵循了以下答案之一中提到的IIS Express的步骤,但是我在原始帖子中提供的代码示例仍然没有得到填充的用户对象。我还更新了applicationhost.config文件以关闭匿名身份验证:

<anonymousAuthentication enabled="false" userName="" />
Run Code Online (Sandbox Code Playgroud)

更新后,我通过邮递员重新提交了测试请求,但出现以下错误:

    <h3>HTTP Error 401.2 - Unauthorized</h3>
    <h4>You are not authorized to view this page due to invalid authentication headers.</h4>
</div>
<div class="content-container">
    <fieldset>
        <h4>Most likely causes:</h4>
        <ul>
            <li>No authentication protocol (including anonymous) is selected in IIS.</li>
            <li>Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.</li>
            <li>Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.</li>
            <li>The Web server is not configured for anonymous access and a required authorization header was not received.</li>
            <li>The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.</li>
        </ul>
    </fieldset>
</div>
<div class="content-container">
    <fieldset>
        <h4>Things you can try:</h4>
        <ul>
            <li>Verify the authentication setting for the resource and then try requesting the resource using that authentication method.</li>
            <li>Verify that the client browser supports Integrated authentication.</li>
            <li>Verify that the request is not going through a proxy when Integrated authentication is used.</li>
            <li>Verify that the user is not explicitly denied access in the "configuration/system.webServer/authorization" configuration section.</li>
            <li>Check the failed request tracing logs for additional information about this error. For more information, click 
                <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>.
            </li>
        </ul>
    </fieldset>
</div>
Run Code Online (Sandbox Code Playgroud)

我是否需要使用某种类型的特殊标头配置邮递员请求才能使其正常工作?

Adr*_*rma 6

除了前面的答案,我们还需要在跨域请求中传递凭据

服务器端(Web API):

SupportsCredentials属性设置true[EnableCors]属性:

[EnableCors(origins: "http://exampleclient.com", headers: "*", 
methods: "*", SupportsCredentials = true)]
Run Code Online (Sandbox Code Playgroud)

客户端(用户界面):

XMLHttpRequest.withCredentials设置为true

jQuery:

$.ajax({
  type: 'get',
  url: 'http://www.example.com/api/auth',
  xhrFields: {
    withCredentials: true
  }
Run Code Online (Sandbox Code Playgroud)

角度:

this.http.get('http://www.example.com/api/auth', { withCredentials: true }).subscribe((resp: any) => {
  console.log(resp)
}
Run Code Online (Sandbox Code Playgroud)

XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('get', 'http://www.example.com/api/auth');
xhr.withCredentials = true;
Run Code Online (Sandbox Code Playgroud)


Hus*_*man 5

如果使用IIS Express,则需要更新applicationhost.config文件。

这是IIS配置工具的文件版本,您可以在其中配置Web服务器本身。您可以在以下目录中找到此文件:

%userprofile%\documents\iisexpress\config\applicationhost.config
Run Code Online (Sandbox Code Playgroud)

要么

%userprofile%\my documents\iisexpress\config\applicationhost.config
Run Code Online (Sandbox Code Playgroud)

找到后,将其更新为:

<windowsAuthentication enabled="true">
    <providers>
        <add value="Negotiate" />
        <add value="NTLM" />
    </providers>
</windowsAuthentication>
Run Code Online (Sandbox Code Playgroud)

对于IIS:

  1. 选择你的应用
  2. 双击-“身份验证”
  3. 启用Windows身份验证
  4. 重新启动IIS服务器

检查此以获取更多详细信息