【发布时间】:2014-12-24 15:29:31
【问题描述】:
所以我一直在寻找我能找到的关于这个主题的所有帖子,但仍然没有弄清楚。希望你能帮忙:
启用 CORS 的 Web API 2:
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
web.config:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
</system.web>
jQuery
<script>
$(document).ready(function()
{
$("button").click(function()
{
var endpoint = "http://localhost:82/api/test";
var base64Credentials = window.btoa("domain\credentials:password");
$.ajax({
url: endpoint,
beforeSend: function(xhr) {
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", "Basic " + base64Credentials);
},
success: function(result) {
alert(result);
}
});
});
});
</script>
IIS 已启用基本身份验证。
基本身份验证适用于 IE。 Firefox 和 Chrome 受预检 OPTIONS 调用的约束。
使用 Fiddler,我可以看到,如果我将 IIS 设置为同时允许匿名身份验证和基本身份验证,那么 OPTIONS 调用将导致 200,然后使用基本身份验证的后续 GET 启动,一切正常。如果我关闭匿名,那么我会在 OPTIONS 调用中收到 401。
问题:
是否需要同时允许匿名和基本身份验证才能支持此用例?
【问题讨论】:
标签: asp.net authentication asp.net-web-api cross-domain