How to enable CORS in ASP.NET Core

前端 未结 12 826
无人及你
无人及你 2020-11-22 14:11

I am trying to enable cross origin resources sharing on my ASP.NET Core Web API, but I am stuck.

The EnableCors attribute accepts policyName

12条回答
  •  被撕碎了的回忆
    2020-11-22 14:20

    Based on Henk's answer I have been able to come up with the specific domain, the method I want to allow and also the header I want to enable CORS for:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
             options.AddPolicy("AllowSpecific", p => p.WithOrigins("http://localhost:1233")
                                                       .WithMethods("GET")
                                                       .WithHeaders("name")));
        services.AddMvc();
    }
    

    usage:

    [EnableCors("AllowSpecific")]
    

提交回复
热议问题