Asp.Net Core API CORS policy error (+308 status code)

278 views Asked by At

I am building a server/client app with C# and React.js

Unfortunately I am facing CORS policy problem: enter image description here

enter image description here

Regarding the server side, I have this code :

var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddPolicy(MyAllowSpecificOrigins,
                          policy =>
                          {
                              policy.WithOrigins("http://localhost:3000")
                                                  .AllowAnyHeader()
                                                  .AllowAnyMethod();
                          });
});



builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<IHttpRequestService, HttpRequestService>();

var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseCors(MyAllowSpecificOrigins);

app.UseAuthorization();

app.MapControllers();

app.Run();

Is someone able to help me understand where are my errors in order to overcome CORS policy issues ?

Thank you in advance, Alexia

1

There are 1 answers

1
aashish On

in asp.net core

services.AddCors(c =>{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());})