我有一个 Ocelot 网关,用于路由和处理我的 API 服务器请求。问题是我在 API 中使用 CORS 策略,当请求从 Web 浏览器发送到 API 时,我需要获取 preflight请求并路由到特定的 API。现在我无法修复 Ocelot 网关中的问题。我该如何解决它?
以下代码是 API 示例:
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/Balance")]
public GeneralResult<DTOResponse> Post(DTORequest req)
{
var result = new GeneralResult<DTOResponse>();
try
{
.
.
.
}
catch (Exception ex)
}
return result;
}
}
最佳答案
经过大量搜索并查看不同的示例后,我找到了问题的解决方案:
正如您在 preflight 中看到的那样请求时,如果从浏览器端调用API,首先会发送预检请求,以保证浏览器与服务器之间连接的真实性。如果我们直接向 API 发出请求,一切正常,但是当我们使用 Ocelot 时,情况就会发生一些变化。在这种情况下,Ocelot 必须识别预检类型请求,据我所知,Ocelot 中未完成此操作,并且必须由开发人员处理。 为了解决这个问题,我首先在ConfigureServices部分使用了以下代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
services.AddCors(o =>
{
o.AddPolicy("CorsPolicy", p =>
{
p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
}
然后在配置部分中,我添加了以下代码:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("CorsPolicy");
app.UsePreflightRequestHandler();
}
正如您在上面的代码片段中看到的,我首先将CORS策略设置添加到ConfigureServices部分中的服务,然后在配置部分中使用它。
然后,我添加了一个名为 UsePreflightRequestHandler 的中间件,它的使用方式是,当从浏览器发送带有选项类型的预检请求时,该请求会在中间件中进行检查,并且所需的 header 已添加到其中。如果请求的类型是选项,则在中间件中检查请求并给出 OK 响应。在这种情况下,浏览器已收到预检请求的适当响应,并且该过程将继续,并且从浏览器发送主请求。
public class PreflightRequestMiddleware
{
private readonly RequestDelegate Next;
public PreflightRequestMiddleware(RequestDelegate next)
{
Next = next;
}
public Task Invoke(HttpContext context)
{
return BeginInvoke(context);
}
private Task BeginInvoke(HttpContext context)
{
context.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
context.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Origin, X-Requested-With, Content-Type, Accept, Athorization, ActualUserOrImpersonatedUserSamAccount, IsImpersonatedUser" });
context.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });
if (context.Request.Method == HttpMethod.Options.Method)
{
context.Response.StatusCode = (int)HttpStatusCode.OK;
return context.Response.WriteAsync("OK");
}
return Next.Invoke(context);
}
}
public static class PreflightRequestExtensions
{
public static IApplicationBuilder UsePreflightRequestHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<PreflightRequestMiddleware>();
}
}
关于c# - 如何在 Ocelot 网关中处理 cors 策略和预检请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72700011/