相关疑难解决方法(0)

Access-Control-Allow-Origin通配符子域,端口和协议

我正在尝试为所有子域,端口和协议启用CORS.

例如,我希望能够从http://sub.mywebsite.com:8080/运行XHR请求到https://www.mywebsite.com/*

通常情况下,我想启用匹配(并限于)的来源请求:

//*.mywebsite.com:*/*

cors

285
推荐指数
5
解决办法
30万
查看次数

.Net Core 配置CORS同时允许所有子域和所有localhost端口

我想允许所有内部网站CORS请求到一个共同的内部API( *.myintra.net),并从所有localhost端口(当我们在IIS Express中,即本地调试各种应用程序http://localhost:12345http://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)

但是该 …

c# cors asp.net-core asp.net-core-3.1

5
推荐指数
1
解决办法
1272
查看次数

标签 统计

cors ×2

asp.net-core ×1

asp.net-core-3.1 ×1

c# ×1