{"id":22888331,"url":"https://github.com/alefranz/headerpropagation","last_synced_at":"2025-05-07T11:45:42.746Z","repository":{"id":60773799,"uuid":"179984104","full_name":"alefranz/HeaderPropagation","owner":"alefranz","description":"ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests.       This is a backport to ASP.NET Core 2.1 (and 2.2) of the ASP.NET Core HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.","archived":false,"fork":false,"pushed_at":"2022-07-07T23:12:56.000Z","size":51,"stargazers_count":31,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T01:39:18.753Z","etag":null,"topics":["aspnetcore","httpclient","httpclientfactory","netstandard"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alefranz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-07T15:18:49.000Z","updated_at":"2024-06-02T11:21:07.000Z","dependencies_parsed_at":"2022-10-04T15:25:56.154Z","dependency_job_id":null,"html_url":"https://github.com/alefranz/HeaderPropagation","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alefranz%2FHeaderPropagation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alefranz%2FHeaderPropagation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alefranz%2FHeaderPropagation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alefranz%2FHeaderPropagation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alefranz","download_url":"https://codeload.github.com/alefranz/HeaderPropagation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252873936,"owners_count":21817708,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspnetcore","httpclient","httpclientfactory","netstandard"],"created_at":"2024-12-13T20:47:43.731Z","updated_at":"2025-05-07T11:45:42.723Z","avatar_url":"https://github.com/alefranz.png","language":"C#","readme":"[![Build Status](https://alefranz.visualstudio.com/HeaderPropagation/_apis/build/status/Build?branchName=master)](https://alefranz.visualstudio.com/HeaderPropagation/_build/latest?definitionId=3\u0026branchName=master) [![](https://img.shields.io/nuget/v/HeaderPropagation.svg)](https://www.nuget.org/packages/HeaderPropagation/)\n\n## About HeaderPropagation\n\nThis is a backport to ASP.NET Core 2.1 (and 2.2) of the\n[HeaderPropagation middleware](https://github.com/aspnet/AspNetCore/pull/7921) I had recently contributed to the\n[ASP.NET Core](https://github.com/aspnet/AspNetCore) project.\nAll code is licensed under the Apache License, Version 2.0 and copyrighted by the [.NET Foundation](https://dotnetfoundation.org/).\n\nIf you are using ASP.NET Core 3.0, please use the official package [Microsoft.AspNetCore.HeaderPropagation](https://www.nuget.org/packages/Microsoft.AspNetCore.HeaderPropagation/).\n\n## Motivation\nI believe it is a common use case which deserves to be included in ASP.NET Core.\nIts main use case is probably to track distributed transaction which requires the ability to pass through a transaction identifier as well as generating a new one when not present.\n\nGiven the ASP.NET Core 3.0 release is quite far away, and the current policy doesn't allow to backport new features to already shipped releases, I have created this package as [recommended](https://github.com/aspnet/AspNetCore/pull/7921#issuecomment-479717164) so it can be used today on projects based on ASP.NET Core 2.1 or 2.2.\n\n## Usage\n\nIn `Startup.Configure` enable the middleware:\n\n```csharp\napp.UseHeaderPropagation();\n```\n\nIn `Startup.ConfigureServices` add the required services, eventually specifying a configuration action:\n\n```csharp\nservices.AddHeaderPropagation(o =\u003e\n{\n    // propagate the header if present\n    o.Headers.Add(\"User-Agent\");\n\n    // if still missing, set it with a value factory\n    o.Headers.Add(\"User-Agent\", context =\u003e \"Mozilla/5.0 (trust me, I'm really Mozilla!)\");\n\n    // propagate the header if present, using a different name in the outbound request\n    o.Headers.Add(\"Accept-Language\", \"Lang\");\n});\n```\n\nIf you are using the `HttpClientFactory`, add the `DelegatingHandler` to the client configuration using the `AddHeaderPropagation` extension method.\n\n```csharp\nservices.AddHttpClient\u003cGitHubClient\u003e(c =\u003e\n{\n    c.BaseAddress = new Uri(\"https://api.github.com/\");\n    c.DefaultRequestHeaders.Add(\"Accept\", \"application/vnd.github.v3+json\");\n}).AddHeaderPropagation();\n```\n\nOr propagate only a specific header, also redefining the name to use\n\n```csharp\nservices.AddHttpClient(\"example\", c =\u003e\n{\n    c.BaseAddress = new Uri(\"https://api.github.com/\");\n    c.DefaultRequestHeaders.Add(\"Accept\", \"application/vnd.github.v3+json\");\n}).AddHeaderPropagation(o =\u003e\n{\n    o.Headers.Add(\"User-Agent\", \"Source\");\n});\n```\n\nSee [samples/WebApplication](samples/WebApplication).\n\n## Behaviour\n\n`HeaderPropagationOptions` contains a dictionary where the key represent the name of the header to consume from the incoming request.\n\nEach entry define the behaviour to propagate that header as follows:\n\n- `InboundHeaderName` is the name of the header to be captured.\n- `CapturedHeaderName` determines the name of the header to be used by default for the outbound http requests. If not specified, defaults to `InboundHeaderName`.\n- When present, the `ValueFilter` delegate will be evaluated once per request to provide the transformed\nheader value. The delegate will be called regardless of whether a header with the name corresponding to `InboundHeaderName` is present in the request. It should return `StringValues.Empty` to not add the header.\n- If multiple configurations for the same header are present, the first which returns a value wins.\n\nPlease note the factory is called only once per incoming request and the same value will be used by all the\noutbound calls.\n\n`HeaderPropagationMessageHandlerOptions` allows to customize the behaviour per clients, where each entry define the behaviour as follows:\n\n- `CapturedHeaderName` is the name of the header to be used to lookup the headers captured.\n- `OutboundHeaderName` is the name of the header to be added to the outgoing http requests. If not specified, defaults to `CapturedHeaderName`.\n\n# Acknowledgements\n\nThis feature would not have been possible without the help of [@rynowak](https://github.com/rynowak) who helped to refine it and get it merged into [ASP.NET Core](https://github.com/aspnet/AspNetCore).\n\nYou can find the [list of contributions](https://github.com/aspnet/AspNetCore/commits/master/src/Middleware/HeaderPropagation) in the original repository.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falefranz%2Fheaderpropagation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falefranz%2Fheaderpropagation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falefranz%2Fheaderpropagation/lists"}