Handle OPTIONS requests in gateway #2107
Replies: 12 comments 2 replies
-
I've solved this problem using a Code for anyone else with the same problem: https://pastebin.com/T2rMDPuT |
Beta Was this translation helpful? Give feedback.
-
With latest ocelot it doesn't work in the |
Beta Was this translation helpful? Give feedback.
-
Thanks @stefankip |
Beta Was this translation helpful? Give feedback.
-
You can define a custom Middleware to set cors reponse header behine the sample code here: public class CorsMiddleware : OcelotMiddleware
{
private readonly GatewayOptions _gatewayOptions;
private readonly OcelotRequestDelegate _next;
public CorsMiddleware(OcelotRequestDelegate next, IOptions<GatewayOptions> options, IOcelotLoggerFactory loggerFactory)
: base(loggerFactory.CreateLogger<UrlBasedAuthenticationMiddleware>())
{
_next = next;
_gatewayOptions = options.Value;
}
public async Task Invoke(DownstreamContext context)
{
context.DownstreamResponse.Headers.Add(string.IsNullOrWhiteSpace(_gatewayOptions.AllowedOrigins)
? new Header(HeaderNames.AccessControlAllowOrigin, new[] { "*" })
: new Header(HeaderNames.AccessControlAllowOrigin,
_gatewayOptions.AllowedOrigins.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)));
context.DownstreamResponse.Headers.Add(new Header(HeaderNames.AccessControlAllowHeaders,
new[] { "*" }));
context.DownstreamResponse.Headers.Add(new Header(HeaderNames.AccessControlRequestMethod,
new[] { "*" }));
await _next(context);
} Ocelot Pipeline: // ...
// We fire off the request and set the response on the scoped data repo
builder.UseMiddleware<HttpRequesterMiddleware>();
// cors
builder.UseMiddleware<CorsMiddleware>(); ------- update ------ |
Beta Was this translation helpful? Give feedback.
-
Hello, Chris! |
Beta Was this translation helpful? Give feedback.
-
Hi Weihan! |
Beta Was this translation helpful? Give feedback.
-
@raman-m the main idea is to set the cors headers for a options request so that the client could continue with the following request such as The previous code sample is going to allow any origin, any http method and any headers, Or we could forward the options request to the downstream also? when downstream does not support OPTIONS request, configure response with default cors headers |
Beta Was this translation helpful? Give feedback.
-
@WeihanLi Welcome back to Ocelot world! 😉 Here are my answers
Why GET and POST only? It should work for any verb I guess which are defined in the route I've reviewed the middleware quickly and... the class methods are not virtual, impossible to override, and I have no whole picture how we could reuse it. We cannot make reflection calls for sure, and the last chance to reuse the functionality 👇
To be discussed... Also, our team needs to look into Yarp code... I'm curious how did Yarp team designed processing OPTIONS. We can borrow ideas 😜
Yep, it is a good start, we can develop more your old code sample... I'm sure we need to re-use MS native implementation somehow.
We have to consider all 3 scenarios by the author! In one of those scenarios Opt request is forwarded to downstream service to make further decisions based on response. To be fair, we must forward because no other ways to check the service by OPTIONS. |
Beta Was this translation helpful? Give feedback.
-
@ggnaegi @RaynaldM |
Beta Was this translation helpful? Give feedback.
-
To support the OPTIONS and pre-flight request, we just add an AddCors services.AddCors();
services.AddOcelot(_config)
// global cors policy
app.UseCors(static x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.DisallowCredentials()
); |
Beta Was this translation helpful? Give feedback.
-
Good idea! We could implement it as a part of Health Checks feature and/or as separate one. |
Beta Was this translation helpful? Give feedback.
-
(This is kind of a combined bug report and feature request; apologies if it's confusing.)
When using CORS with Ocelot and a bunch of microservices, we would like to have Ocelot handle all the CORS stuff and disable it in the downstream services.
I've tried to achieve this a few ways:
Expected Behavior
Access-Control-Allowed-Origin
*, which Ocelot would replace with the correct value (the origin that was allowed)Actual Behavior
Access-Control-Allowed-Origin
header returned to the client contains * which is insecureAccess-Control-Allowed-Origin
header, but return 405 to the OPTIONS request because the Asp.Net Core CORS middleware isn't runningI think 3 would be the best solution. I can probably simulate this by writing a middleware of my own but think this should be handled by Ocelot.
Beta Was this translation helpful? Give feedback.
All reactions