我有一个JS客户端和一个在不同主机上运行的WEB API 2.2服务 .
该服务需要Windows身份验证,并且由于服务位于不同的主机上,因此还必须启用CORS . JS客户端需要对服务执行XHR GET请求(没有任何自定义头,因此不需要CORS预检)
I am having trouble getting Internet Explorer 11 (11.576.14393.0) to work (neither version 10 does). 请注意,Chrome(55.0.2883.87 m)和Firefox(50.0.2)可以正常工作,而MS Edge则不行(38.14393.0.0)
我在web.config中启用了Windows身份验证和授权 . 目前,它只需要一个有效的Windows用户 . 请注意,OPTIONS动词配置已注释掉,因为不需要预检 .
<system.web>
<authentication mode="Windows" />
<authorization>
<!--<allow verbs="OPTIONS" users="*"/>-->
<deny users="?"/>
</authorization>
</system.web>
我最初使用Microsoft.AspNet.WebApi.Cors NuGet包启用了CORS,但是在启用Windows身份验证时它没有将CORS头返回给客户端,因此,为简单起见,我只是在web.config中添加了CORS头 . 每次请求返回到Web API:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://192.168.100.2" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
当然,我已经将“Access-Control-Allow-Origin”标头的值设置为JS客户端的域 .
我的API控制器非常简单,如下所示:
public class DefaultController : ApiController
{
[HttpGet]
public string Method()
{
return "api response";
}
}
使用jQuery 2.2.4调用JS客户端AJAX:
$().ready(function() {
$.ajax({
url: "http://192.168.100.73/WinAuthAndCors.WebAPI/api/default/method",
method: "GET",
success: function(data) {
console.log("success: " + data);
},
error: function() {
console.log("error");
},
xhrFields: {
withCredentials: true
}
});
});
请注意,问题中的代码片段是对普通客户端和Web API项目进行的所有自定义,其余部分是样板代码 .
Here is the issue:
Internet Explorer发出GET请求,正确执行CORS检查,但随后放弃了预期的401响应,并且不继续执行Windows身份验证协商 .
-
禁用Windows身份验证 - > IE工作正常
-
在与客户端相同的主机上移动服务(有效禁用CORS) - > IE工作正常
例如,Chrome检查CORS,然后提示输入用户凭据,然后正确完成XHR(在后台进行Win Auth协商时)
Am I missing something or is IE simply unable to handle this case of CORS with Windows Authentication?
稍后编辑:我试图尽可能缩小问题范围,因此我在Node中创建了一个简单的HTTP服务器,并为第一个请求返回了以下标头 . Chrome会提示输入凭据,而IE则不会执行任何操作 .
response.writeHead(401, {
"WWW-Authenticate": "Negotiate",
"WWW-Authenticate": "NTLM",
"Access-Control-Allow-Origin": "http://192.168.100.2:8087",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Credentials": "true"
});