Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 5 AddHeaderPropogation not forwarding RequestHeaders

In C# Web Api I am attempting to forward 100% of all the headers and params arriving in an HTTP request. There are a wide range of preexisting Requests formats in the solution. Perhaps 60 different API types, each with different structures. I don't know the header contents of each Request type. However, hopefully that is immaterial to the problem. Firstly, I validate the Authorization Header of the inbound request. If is it valid, I want to pass ALL Headers, plus the query string, to the final endpoint "as-is". The final endpoint is in a PHP/Laravel server, with the plan to selectively move each endpoint to a C# server. I mention this in case you would ask, "Why do you want to do that?"

The "forward propagations" issue for the unknown Headers appears to have a solution in Microsoft's

 services.AddHttpClient("ApiUserCheckSkills").AddHeaderPropagation(); on Factory generated Clients
 app.UseHeaderPropagation(); 
 // Both activated in Startup.cs

From what I have read .AddHeaderPropagation(), WITHOUT arguments, will default to propagating every Header item, unmodified and anonymously, into the next request. Perfect! But it isn't working for me.

Inside the Controller that will accept and forward the initial Request:

```
    public UserAuthGateway(IHttpClientFactory factory)
    {
        _factory = factory;
    }
    
    [Route("api/user/check/skills")]
    [HttpGet]
    public async Task<IActionResult> ApiUserCheckSkills([FromQuery] string queryString)
    {

        //
        //  This call require an Authenticated User identified by a Bearer Token as well as an expires_in integer format seconds 
        //             
        string validationResult = Methods.JWS.JWS_ValidateAsymmetricAsGate(Request.Headers, Keys.publicXmlKey);
        if (!(validationResult == "BearerTokenValid"))
        {
            return StatusCode(401, validationResult);  // Some Problem with Authorization
        }            
        // Assuming the auth token was valid, here is the forwarding code
         var request ="https://xxx.xxxxx.systems/api/user/check/skills" + Request.QueryString;
        var client = _factory.CreateClient("ApiUserCheckSkills");
        var response = await client.GetAsync(request, 0 );
        
        if (response.IsSuccessStatusCode)
        {
            var ret = await response.Content.ReadAsStringAsync();               
            return Ok();
        }
        else
        {
            return StatusCode(500, "Something Went Wrong! Error Occured");
        }
       


    } 
var client = _factory.CreateClient("ApiUserCheckSkills"); 

Has no Headers. Therefore, the call to the final endpoint fails. The call is reaching the final end point. But it fails because validation of the Bearer token, intended to be propagated in the Authorization header, is now missing. It has not been propagated. There are a number of examples on Google of HeaderPropagation. The capability of using the AddHeaderPropagation() with arguments is often demonstrated with specific headers but no examples of Propagation of all anonymous headers.

like image 730
user3594395 Avatar asked Nov 01 '25 20:11

user3594395


1 Answers

You have to specify all headers you would like to propagate. Here you can find more samples.

public void ConfigureServices(IServiceCollection services)
{
   // Define all headers you would like to propogate
   services.AddHeaderPropagation(options => options.Headers.Add("sample-header"));

   services
      .AddHttpClient("externalapi-client", options => options.BaseAddress = new Uri("https://localhost:5001"))
      // Define DelegatingHandler which handles before sending request to external Api
      .AddHeaderPropagation();

   services.AddControllers();
}
like image 111
Valdis Pavlukevics Avatar answered Nov 03 '25 09:11

Valdis Pavlukevics