[英]proper implementation of “windows” authentication in web api?
我创建了一个 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 请求才能使其正常工作?
除了前面的答案,我们还需要在跨域请求中传递凭据。
服务器端(Web API):
在[EnableCors]
属性上将SupportsCredentials属性设置为true
:
[EnableCors(origins: "http://exampleclient.com", headers: "*",
methods: "*", SupportsCredentials = true)]
客户端(用户界面):
将XMLHttpRequest.withCredentials设置为true
。
jQuery:
$.ajax({
type: 'get',
url: 'http://www.example.com/api/auth',
xhrFields: {
withCredentials: true
}
角度:
this.http.get('http://www.example.com/api/auth', { withCredentials: true }).subscribe((resp: any) => {
console.log(resp)
}
XMLHttpRequest:
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://www.example.com/api/auth');
xhr.withCredentials = true;
如果您使用的是IIS Express ,则需要更新applicationhost.config
文件。
这是 IIS 配置工具的文件版本,您可以在其中配置 Web 服务器本身。 您可以在以下目录中找到此文件:
%userprofile%\documents\iisexpress\config\applicationhost.config
或
%userprofile%\my documents\iisexpress\config\applicationhost.config
找到后,将其更新为:
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
对于 IIS:
检查这个以获取更多详细信息
使用本地域用户并用于 Intranet 站点的 Windows 身份验证。
示例:
我实现了一个带有固定路由路径的TestAuthentication
方法/操作。 对于演示,我还没有包含 Authorize 属性。 该代码检查ApiController
的User
属性。 这包含与Thread.CurrentPrincipal
或HttpContext.Current.User
相同的数据。 确保禁用 IIS 中的匿名身份验证,否则Identity.Name
将为空。
public class WinAuthController : ApiController
{
[HttpGet]
[Route("api/testauthentication")]
public IHttpActionResult TestAutentication()
{
Debug.Write("AuthenticationType:" + User.Identity.AuthenticationType);
Debug.Write("IsAuthenticated:" + User.Identity.IsAuthenticated);
Debug.Write("Name:" + User.Identity.Name);
if (User.Identity.IsAuthenticated)
{
return Ok("Authenticated: " + User.Identity.Name);
}
else
{
return BadRequest("Not authenticated");
}
}
}
在 Web.config 文件中:
<system.web>
<authentication mode="Windows" />
</system.web>
在 IE 中,您可以使用工具 > Internet 选项 > 高级检查设置,然后查找启用 Windows 集成身份验证的设置。 当您转到选项卡安全,然后是内联网和自定义级别时,您会在底部找到一个设置,用于指定 IE 是否应自动登录或提示输入用户名和密码。
请访问以下链接,WEP API Windows 身份验证有正确的步骤:
以下是在 web api 中为本地和服务器 (IIS) 配置 Windows 身份验证的步骤。
1)对于本地:
a) 在 windows 认证模式下创建 web api 项目,请按照以下步骤操作:
选择ASP.Net Web Application 后,选择Web API 模板,然后从右侧单击更改身份验证按钮并选择Windows 身份验证。
b) 对于现有的 web api 项目,只需在applicationhost.config
文件中添加以下行。
<location path="YourProjectName">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
2)对于服务器 (IIS)
要在 IIS 中托管应用程序后运行 Windows 身份验证,只需在system.web
节点内的web.config
文件中添加以下行:
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="?" />
<deny users="?" />
</authorization>
在这两种情况下,只需在 Windows 身份验证正常工作的代码中使用以下几行:
if(User.Identity.IsAuthenticated)
{
//do work
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.