【问题标题】:.Net Core Configure CORS to allow all subdomains and all localhost ports at the same time.Net Core 配置 CORS 以同时允许所有子域和所有 localhost 端口
【发布时间】:2020-06-21 20:04:34
【问题描述】:

我想允许来自所有内部网站 (*.myintra.net) 以及所有 localhost 端口的 CORS 请求对一个公共内部 API 进行请求(当我们在 IIS Express 中本地调试各种应用程序时,即 http://localhost:12345http://localhost:54321 等)。

This answer 向我展示了如何使用SetIsOriginAllowedToAllowWildcardSubdomains() 来允许所有子域。

This answer 向我展示了如何使用SetIsOriginAllowed() 委托函数来允许任何localhost 端口。

但是,这些选项似乎不能一起使用。我的配置:

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"
            ...();
        });
    });
    ...
}

我可以交换配置的顺序:

.SetIsOriginAllowed(origin => AllowLocalhost(origin))
.SetIsOriginAllowedToAllowWildcardSubdomains()

但是AllowLocalhost() 函数永远不会被调用。我想一次只有一个工作是有道理的,因为第一次检查可能会返回true,而第二次检查可能会返回false

理想情况下,我想要一个不需要我在 AllowLocalhost() 函数中重新实现 allow wildcard 逻辑的解决方案。

另外值得注意的是,我真的只需要在开发环境中使用它。生产需要允许通配符,但无论端口如何,都不允许使用localhost

【问题讨论】:

标签: c# asp.net-core cors asp.net-core-3.1


【解决方案1】:

这就是我实现它的方式:

我正在使用 IsOriginAllowed 检查原点:

    public void ConfigureServices(IServiceCollection services)
    {
        // ...

        services.AddCors(options =>
        {
            options.AddPolicy(name: "AllowedCorsOrigins",
                builder =>
                {
                    builder
                        .SetIsOriginAllowed(IsOriginAllowed)
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowCredentials();
                });
        });
        // ...

一如既往地激活 Cors:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
        // ...
        app.UseCors("AllowedCorsOrigins");
        // ...

这就是方法,应该可以解决问题。 接受来自 example.com 和 another-example.com 的所有请求,包括所有子域。 如果服务正在运行的 ASPNETCORE_ENVIRONMENT 包含“DEV”,那么也允许 localhost。

private static bool IsOriginAllowed(string origin)
{
    var uri = new Uri(origin);
    var env = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "n/a";

    var isAllowed = uri.Host.Equals("example.com", StringComparison.OrdinalIgnoreCase)
                    || uri.Host.Equals("another-example.com", StringComparison.OrdinalIgnoreCase)
                    || uri.Host.EndsWith(".example.com", StringComparison.OrdinalIgnoreCase)
                    || uri.Host.EndsWith(".another-example.com", StringComparison.OrdinalIgnoreCase);
    if (!isAllowed && env.Contains("DEV", StringComparison.OrdinalIgnoreCase))
        isAllowed = uri.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase);

    return isAllowed;
}

【讨论】:

  • 这是否允许 http 和 https 请求?如果是这样,最好添加一个额外的检查:uri.Scheme == Uri.UriSchemeHttps 以防止 http 调用(如果需要)。
  • 很好的答案!正是我需要的。
猜你喜欢
  • 2016-08-21
  • 2015-01-24
  • 2019-08-01
  • 1970-01-01
  • 2022-10-05
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
相关资源
最近更新 更多