-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpContext lost between HttpClientWrapper and DelegatingHandler #1252
Comments
Same issue here, version 16.0.1 |
Hello, I've proposed a solution (see pull request #1268 ) where the proper HttpContext will be added as a property to a delegating handler class if the class implements the specified interface. |
I tried the code submitted in #1268 (by implementing IDelegatingHandlerWithHttpContext in my DelegatingHandler) but I experienced that HttpContext.User.Claims were not updated after the first request (so any subsequent requests would use the same user claims). Steps to reproduce:
Is this a scenario you've tested? My workaround-hack for getting user claims to be available in the And when I needed that data in the |
@bjartekh thanks, you're right. There's a problem with caching. I'll look into it if I find time. Unfortunately Ocelot seems to be abandoned after breaking changes with HttpContext :/ |
@bjartekh if you are interested, I've just fixed the problem with cache. |
The Ocelot project seems dead, but if anyone experiences this issue, here is a workaround. Set the
// Startup.cs
services.AddHttpContextAccessor();
// Startup.cs
app.UseOcelot(oc =>
{
oc.PreQueryStringBuilderMiddleware = async (context, next) =>
{
var contextAccessor = context.RequestServices.GetRequiredService<IHttpContextAccessor>();
contextAccessor.HttpContext = context;
await next.Invoke();
};
}).Wait();
Why it works Learn more about AsyncLocal. |
Hi @AgentShark ! Does it solve the problem with user claims (aka What about to make it as an additional method for the IOcelotBuilder interface of DependencyInjection part of Ocelot? services
.AddOcelot()
.AddHttpContextAccessor(); or app.UseOcelot()
.UseHttpContextAccessor(); |
@amweiss Hi Adam! I would like personally to thank you for reporting this issue being found in v15.x! Yeah, it was a large upgrade to .NET 5 with a lot of breaking changes made by Tom. Are you still interested to collaborate on this issue?
Are you able to upgrade your solution to .NET 7 with usage of Ocelot v19.0.2 (latest ver.)? @davipsr and @bjartekh @AgentShark |
I no longer use this project so I won't be going back to verify. |
@amweiss
Sorry for disturbing you! You can unsubscribe from notifications for this issue. |
@raman-m I use v18.0.0 where the issue is still present. |
@AgentShark wrote on June 26, 2023:
How can we verify your solution? Do you have some time for contribution?
Okay... It seems you misunderstood me. app.UseOcelot(oc =>
{
oc.PreQueryStringBuilderMiddleware = async (context, next) =>
{
var contextAccessor = context.RequestServices.GetRequiredService<IHttpContextAccessor>();
contextAccessor.HttpContext = context;
await next.Invoke();
};
}).Wait(); ...as Finally we need to update Ocelot docs:
Microsoft Learn: Access HttpContext in ASP.NET Core |
TODO
|
+ Accepted ...due to open PR #1268 |
@ggnaegi, FYI, the linked pull request #1268 was closed a few days ago due to a significant number of merge conflicts. |
@raman-m Do we have this issue in a multiplexing context? |
Expected Behavior / New Feature
A DelegatingHandler with
IHttpContextAccessor
injected can access the current context correctly.Actual Behavior / Motivation for New Feature
As of version 15.0.7, a custom
DelegatingHandler
we use now only has access to an empty context and thushttpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
is now null instead of the name like it was in 15.0.6Steps to Reproduce the Problem
DelegatingHandler
that takes inIHttpContextAccessor
as an injected dependency.SendAsync
method, callhttpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
Specifications
The text was updated successfully, but these errors were encountered: