Enable OPTIONS header for CORS on .NET Core Web API

后端 未结 7 1537
温柔的废话
温柔的废话 2020-11-28 06:55

I solved this problem after not finding the solution on Stackoverflow, so I am sharing my problem here and the solution in an answer.

After enabling a cross domain p

相关标签:
7条回答
  • 2020-11-28 07:49

    I want to put a specific answer for my specific situation where i was testing both the api and the client web app locally. I know this is a late entry but CORS has changed so much in dot net core, i thought, newcomers like me might benefit with a full post.

    For me, it was two issues that occurred back to back.

    1. CORS rejection error
    2. and also OPTIONS issue on firefox (I assume chrome would do the same)
    3. also my API is running HTTPS
    4. web app is without HTTPS
    5. both of them running locally, mentioning this again, for clarity.

    First, this goes to public void ConfigureServices(IServiceCollection services)

            //lets add some CORS stuff 
            services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder => {
                    builder.WithOrigins("http://localhost:3000",
                                        "http://www.contoso.com");
                    builder.AllowAnyMethod();
                    builder.AllowAnyHeader();
                    builder.AllowCredentials();
                });
            });
    

    and then, this, goes to, public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

      app.UseCors();
    
    0 讨论(0)
提交回复
热议问题