skip to Main Content

I have implemented Web API using .net core 8.0 . I already have implemented CORS to allow all origin for time being. API is working fine on localhost. but when I publish API on Azure,it is giving me CORS error.

I have refer below blogs

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0

Dot Net Core Web API with Javascript – CORS policy error

.Net Core 6 web api Cors

2

Answers


  1. need to configure cors policy for you urls in program.cs

    builder.Services.AddCors(options =>
    {
        options.AddPolicy(name: "allowedregions",
                          policy  =>
                          {
                              policy.WithOrigins("http://example.com",
                                                  "http://www.test.com");
                          });
    });
    

    also configure middle ware

    app.UseCors("allowedregions");
    

    more you can find here

    Login or Signup to reply.
  2. You need to allow your portal url in Program.CS file by adding CORS policy.

    builder.Services.AddCors(options =>
    {
    options.AddPolicy("AllowSpecificOrigin",
                builder =>
                {
                    builder.WithOrigins("http://localhost:4200", "http://localhost:3000", "https:yourreactpublishedurl")
                           .AllowAnyHeader()
                           .AllowAnyMethod()
                           .AllowCredentials();
                });
        });
    
    app.UseCors("AllowSpecificOrigin");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search