Search code examples
c#corsblazor-webassemblyocelotminimal-apis

Disabling CORS in Minimal API Ocelot Gateway


So the problem is that i have Blazor WebAssembly for a front-end, Making API calls through the Ocelot API Gateway but for some reason the CORS are failing

enter image description here

but in Ocelot Gateway in Program.cs i have

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
    .AddJsonFile("ocelot.json", optional: false, reloadOnChange:true)
    .AddEnvironmentVariables();

builder.Services.AddOcelot(builder.Configuration);

var app = builder.Build();
await app.UseOcelot();

app.UseCors(builder => builder
    .AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader());

app.Run();

Having like this also does not work:

builder.Services.AddCors(options =>
{
    options.AddPolicy("asd",  
        policy =>  
        {  
            policy.AllowAnyOrigin().AllowAnyMethod();
        });  
});

var app = builder.Build();
await app.UseOcelot();
app.UseCors("asd");
app.Run();

What should I do to access any origin or just get rid of this cors? I have tried nearly everything but nothings seems to solve my problem.


Solution

  • Solution:

    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)
        .AddJsonFile("ocelot.json", optional: false, reloadOnChange:true)
        .AddEnvironmentVariables();
    
    builder.Services.AddOcelot(builder.Configuration);
    
    builder.Services.AddCors(); // Add cors
    
    var app = builder.Build();
    app.UseCors(builder => builder // Allow any
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());
    
    await app.UseOcelot();
    app.Run();