我正在尝试为所有子域,端口和协议启用CORS.
例如,我希望能够从http://sub.mywebsite.com:8080/运行XHR请求到https://www.mywebsite.com/*
通常情况下,我想启用匹配(并限于)的来源请求:
//*.mywebsite.com:*/*
我想允许所有内部网站CORS请求到一个共同的内部API( *.myintra.net),并从所有localhost端口(当我们在IIS Express中,即本地调试各种应用程序http://localhost:12345,http://localhost:54321等等)。
这个答案向我展示了如何使用SetIsOriginAllowedToAllowWildcardSubdomains()以允许所有子域。
这个答案向我展示了如何localhost使用SetIsOriginAllowed()委托函数来允许任何端口。
但是,这些选项似乎不能一起使用。我的配置:
private bool AllowLocalhost(string origin)
{
var uri = new Uri(origin);
return (uri.Host == "localhost");
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
string[] corsList = "https://*.myintra.net,https://some.specificurl.com".Split(",");
options.AddPolicy("CorsPolicy", builder =>
{
builder
.WithOrigins(corsList.ToArray())
.SetIsOriginAllowedToAllowWildcardSubdomains()
.SetIsOriginAllowed(origin => AllowLocalhost(origin)) // disallows calls from myapp.myintra.net since it doesn't uri.Host match "localhost"
...();
});
});
...
}
Run Code Online (Sandbox Code Playgroud)
我可以交换配置的顺序:
.SetIsOriginAllowed(origin => AllowLocalhost(origin))
.SetIsOriginAllowedToAllowWildcardSubdomains()
Run Code Online (Sandbox Code Playgroud)
但是该 …