简体   繁体   中英

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

在此处输入图像描述

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:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM