簡體   English   中英

在最小 API Ocelot 網關中禁用 CORS

[英]Disabling CORS in Minimal API Ocelot Gateway

所以問題是我的前端有 Blazor WebAssembly,通過 Ocelot API 網關進行 API 調用,但由於某種原因 CORS 失敗

在此處輸入圖像描述

但是在Program.csOcelot Gateway中我有

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

像這樣也行不通:

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

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

我應該怎么做才能訪問任何來源或擺脫這個 cors? 我幾乎嘗試了所有方法,但似乎沒有什么能解決我的問題。

解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM