EnableCors 总是 return Web 中允许的来源的通配符 Api
EnableCors always return wildcard for allowed origins in Web Api
我有一种情况是将 ajax post 请求从 http 发送到 https(出于某些复杂的原因必须这样做),并且为了 cookie,我需要传递凭据。
我在服务器端使用 Web Api,使用 NuGet 包 Microsoft.AspNet.WebApi.Cors 来启用 Cors 请求。
客户端代码:
$.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
预检请求header:
Access-Control-Request-Headers: accept, __AntiXsrfTokenKey, content-type
Access-Control-Request-Method: POST
Origin: http://www.sitename.com
响应header:
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 header 返回的是通配符 *,而不是我在控制器“http://www.sitename.com”的属性中指定的请求来源。所以我结束了错误:
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 属性中正确设置 Access-Control-Allow-Origin 域?
好的,终于在 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>
我有一种情况是将 ajax post 请求从 http 发送到 https(出于某些复杂的原因必须这样做),并且为了 cookie,我需要传递凭据。
我在服务器端使用 Web Api,使用 NuGet 包 Microsoft.AspNet.WebApi.Cors 来启用 Cors 请求。
客户端代码:
$.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
预检请求header:
Access-Control-Request-Headers: accept, __AntiXsrfTokenKey, content-type
Access-Control-Request-Method: POST
Origin: http://www.sitename.com
响应header:
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 header 返回的是通配符 *,而不是我在控制器“http://www.sitename.com”的属性中指定的请求来源。所以我结束了错误:
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 属性中正确设置 Access-Control-Allow-Origin 域?
好的,终于在 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>