Asp.net web api EnableCor始终为Web Api中允许的源代码返回通配符
我有一个场景可以将ajax post请求从http发送到https(出于一些复杂的原因必须这样做),为了cookies,我需要传递凭据 我在服务器端使用Web Api,并使用NuGet包Microsoft.AspNet.WebApi.Cors来启用Cors请求 客户端中的代码:Asp.net web api EnableCor始终为Web Api中允许的源代码返回通配符,asp.net-web-api,cors,Asp.net Web Api,Cors,我有一个场景可以将ajax post请求从http发送到https(出于一些复杂的原因必须这样做),为了cookies,我需要传递凭据 我在服务器端使用Web Api,并使用NuGet包Microsoft.AspNet.WebApi.Cors来启用Cors请求 客户端中的代码: $.ajax({ url: 'https://www.sitename.com/api/xxx', type: 'post', data: {
$.ajax({
url: 'https://www.sitename.com/api/xxx',
type: 'post',
data: {
'': 1
},
headers: {
'__AntiXsrfTokenKey': 'whatevertokenhere'
},
xhrFields: {
withCredentials: true
},
dataType: 'json',
success: function (data) {
}
});
Web Api设置:
config.EnableCors();
控制器的EnableCors属性:
[EnableCors(origins: "http://www.sitename.com", headers: "*", methods: "*", SupportsCredentials = true)]
public class XXXController : ApiController
飞行前请求标头:
Access-Control-Request-Headers: accept, __AntiXsrfTokenKey, content-type
Access-Control-Request-Method: POST
Origin: http://www.sitename.com
响应标题:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: __AntiXsrfTokenKey,content-type
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: *
问题很明显,Access Control Allow Origin标头返回的是通配符*,而不是我在控制器“”的属性中指定的请求源。因此,我结束了这个错误:
A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://www.sitename.com' is therefore not allowed access.
有人知道为什么会出现这种行为,以及我如何在EnableCors属性中正确设置访问控制允许源域吗?好的,最后在system.webServer中的web.config中找到了这一点
<rewrite>
<outboundRules>
<rule name="Set Access-Control-Allow-Origin header">
<match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern="(.*)" />
<action type="Rewrite" value="*" />
</rule>
</outboundRules>
</rewrite>