【发布时间】:2018-09-07 19:07:49
【问题描述】:
我创建了一个仅用于公司网络的 Web Api 2 应用。我已经阅读了有关 Web API 中的 Windows 身份验证的信息,所以这似乎是可能的。但我需要为此找出正确的实现方式。我在我的 Web.config 中包含了以下 xml:
<system.web>
<authentication mode="Windows" />
</system.web>
我似乎记得老式网络表单应用程序中的某种类型的事件挂钩。像 BeginRequest() 这样的东西,可以在呈现页面之前进行安全检查。我在我的一个控制器方法中包含以下代码行作为第一行,但返回的值似乎只是一个没有任何有意义信息的空对象:
var identity = HttpContext.Current.User.Identity as WindowsIdentity;
Web API 2 是否支持 Windows 身份验证?我错过了一步吗?如果我提交来自 Postman 的一般测试请求,Windows 身份验证是否有效?我也试过这段代码,但得到了一个类似的空对象:
var x = RequestContext.Principal;
我隐约记得像“启用集成安全性”这样的 IIS 设置。你能指定确切的设置吗?如果我在 IIS Express 上运行应用程序,我能做到这一点吗?
更新
我按照以下答案之一中提到的 IIS Express 的步骤操作,但我在原始帖子中提供的代码示例仍然没有得到填充的用户对象。我还更新了 applicationhost.config 文件以关闭匿名身份验证:
<anonymousAuthentication enabled="false" userName="" />
更新后,我通过 Postman 重新提交了测试请求,但出现以下错误:
<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>
我是否需要使用某种类型的特殊标头配置我的 Postman 请求才能使其正常工作?
【问题讨论】:
标签: c# .net asp.net-web-api asp.net-web-api2 windows-authentication