AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

3.1 CORS is not working with Web API

Open WaseemAhmadNaeem opened this issue 6 years ago • 4 comments

.NET Core 3.1, CORS is not Working with Web API

CORS is not working with .NET Core 3.1 Web API. I followed exact instructions provided at Official Documentation and Tutorial

Links I Followed:

In Addition I tried every possible location of app.UseCors(PolicyName)

Software versions

  .NET Core SDK (reflecting any global.json):
  Version:   3.1.100
  Commit:    cd82f021f4

 Runtime Environment:
  OS Name:     Windows
  OS Version:  10.0.18362
  OS Platform: Windows
  RID:         win10-x64
  Base Path:   C:\Program Files\dotnet\sdk\3.1.100\      

 Host (useful for support):
   Version: 3.1.0
   Commit:  65f04fb6db 

Code to reproduce the issue

Configure Services

 public void ConfigureServices(IServiceCollection services)
        {

            services.AddCors(options =>
            {
                options.AddPolicy(MyAllowedOrigins,
                builder =>
                {
                    builder.AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                });
            });
//other code
}

Configure

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();


            app.UseRouting();

            app.UseCors(MyAllowedOrigins);

            // app.UseAuthentication();
            // app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers().RequireCors(MyAllowedOrigins);

            });
        }
    }

WaseemAhmadNaeem avatar Jan 31 '20 20:01 WaseemAhmadNaeem

Hello! Thanks for contacting us. This sounds like a general question about using ASP.NET Core. While we try to look at and respond to all issues, for questions like this we recommend posting to a community support group like Stack Overflow with the asp.net-core tag.

Potential bugs in the ASP.NET Core framework and product feedback can be reported at dotnet/aspnetcore issues (include full repro steps and cross-link to a repro project in GH when appropriate). For Visual Studio, use the Report a Problem gesture or Suggest a Feature gesture within VS, which open internal issues for the VS team. For Visual Studio Code, you can open an issue at microsoft/vscode issues.

Rick-Anderson avatar Feb 03 '20 03:02 Rick-Anderson

I followed exact instructions provided at Official Documentation and Tutorial

Where?

Rick-Anderson avatar Feb 03 '20 03:02 Rick-Anderson

Web API is using app.UseHttpsRedirection(); which cause CORS issue if requesting client is not https based. So in order to use it with http client we need to comment or remove that line.

This issue is not with CORS, the https is causing this issue but thrown error is saying its with CORS. Documentation should be improved

WaseemAhmadNaeem avatar Feb 04 '20 08:02 WaseemAhmadNaeem

We recommend always using HTTPS

Rick-Anderson avatar Feb 04 '20 19:02 Rick-Anderson