ASP.NET 5 WebApi 选项预检时不允许使用 405 方法

405 Method Not Allowed on ASP.NET 5 WebApi OPTIONS preflight

我正在尝试在我的应用程序中集成 CORS 策略。 为此,我需要使用两个策略,因此我按照 Enable CORS with Attributes 上的说明进行了以下操作。作为记录,我将 CORS 配置包装在一个扩展中,并且我正在从配置中加载列入白名单的端点。

    public static IServiceCollection ConfigureCors(this IServiceCollection serviceCollection,
        IConfiguration configuration)
    {
        return serviceCollection
                .AddCors(cors => cors.AddPolicy(CORS_POLICY_STRICT, policy =>
                    policy
                        .WithOrigins()
                        .WithMethods("POST")
                        .WithHeaders("Authorization")
                ))
                .AddCors(cors =>
                    cors.AddPolicy(CORS_POLICY_FE_API, policy =>
                        policy
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .WithOrigins(GetFrontEndOrigins(configuration))
                    ))
            ;
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

           app
            .UseHttpsRedirection()
            .UseRouting()
            .UseCors()
            .UseAuthentication()
            .UseAuthorization()
            .UseEndpoints(endpoints => { endpoints.MapControllers(); })
            ;
    }


    public void ConfigureServices(IServiceCollection services)
    {
        services
            .ConfigureCors(Configuration)
            .AddControllers()
            ;
    }

CORS_POLICY_STRICT 应该是 BE-2-BE 调用的策略,而 CORS_POLICY_FE_API 必须征用 localhost 和 prod+dev FE 微服务 URLs.

我在适当的地方用 [EnableCors(<POLICY_NAME_CONST>)] 注释了控制器。

我的问题是应用程序启动时没有错误,Swagger 可以工作,但我什至无法 运行 cUrl/PowerShell 中的预检请求,也无法从 Angular 应用程序调用代码部署在本地主机的另一个端口(4200,通常)。

选择任何用 EnableCorsAttribute

注释的 API URL
> Invoke-RestMethod -Method Options -Uri "https://localhost:5001/api/v1/secure/SomeController/someMethod" -Headers @{"Access-Control-Request-Method"="POST";"Access-Control-Request-Headers"="X-Requested-With"}

$ curl -X OPTIONS "https://localhost:5001/api/v1/secure/SomeController/someMethod" -i   

两者都 return 405 方法不允许

这有什么问题?

我还尝试将简单的 UseCors() 调用替换为对 UseCors(string) 的两次调用,每个调用都包含一个策略键。只有策略名称是参数,因为我将从服务配置策略。

最终成功了,因为我在 http://localhost :4200 中配置了一个不需要的 space 作为允许的来源之一