Skip to content

I can't add new header or change the authorization token for the downstream request #2321

@siualpinto

Description

@siualpinto

Hi
I need to change the access token of requests that go to the APIs via the Ocelot API Gateway.
I try to use a DelegatingHandler or PreQueryStringBuilderMiddleware/PreAuthorizationMiddleware, and the behaviour is the same.
Both change the Header in the request, but when I receive the request in the API, it seems that it uses all the original Headers. None of the new headers that I add reach the API.
Even tried to use it via configuration using the DownstreamHeaderTransform or UpstreamHeaderTransform in the route, but nothing.

Middleware

TokenResult tokenResult = ...;
 DownstreamRequest downstreamRequest = httpContext.Items.DownstreamRequest();
 httpContext.Request.Headers.Authorization = tokenResult.AccessToken;
// or 
 downstreamRequest.Headers.TryAddWithoutValidation("Authorization", $"Bearer {tokenResult.AccessToken}");

 _logger.LogInformation($"New Token: {httpContext.Request.Headers.Authorization}");

Delegating handler

TokenResult tokenResult = ...;
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.AccessToken);

Is there some configuration that I'm missing to enable the injection of Headers?


Specifications

Version: 24.0.1
Platform: .NET 8
Subsystem: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionInitially seen a question could become a new feature or bug or closed ;)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions