Request Header Propagation In .NET Core Web Applications
Request header propagation .NET Core App Demo

Request Header Propagation In .NET Core Web Applications

In this short article, let’s have a look at a simple middleware to propagate the request headers.

Why ?

Application might be interacting with many backend APIs using HttpClient instances. Those instances might require specific request headers to be present in the request. And as we have seen in previous blog posts, those headers can be added via AddHttpClient middleware as shown in below code snippet.

services.AddHttpClient<GitHubApionsumer>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");
// Github API versioning
c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
// Github requires a user-agent
c.DefaultRequestHeaders.Add("User-Agent", "theCodeBlogger.com Demo");
}).SetHandlerLifetime(new TimeSpan(0, 10, 0)); // Handler Lifetime is set to 10 minutes
view raw Startup.cs hosted with ❤ by GitHub

There might be some cases where an application is required to send the incoming request headers to the backend APIs.

There can be three different use cases where request header propagation:

  • Forward header as-is
  • Forward a header with different name
  • Generate a new header or conditionally generate header value

For all such scenarios, header propagation middleware can be used.

How ?

If an application needs to use header propagation middleware, it should refer to a NuGet package Microsoft.AspNetCore.HeaderPropagation. Once this reference is added, the middleware can be configured in application startup as shown below.

// Startup for .NET Core application
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Common Propagation Configuration
services.AddHeaderPropagation(options =>
{
// Propagate X-INBOUND-HEADER if it is present
options.Headers.Add("X-INBOUND-HEADER");
// Propagate X-HEADER with its value same as value of X-CUSTOM-HEADER
options.Headers.Add("user-agent", "X-RENAMED-HEADER");
// X-CUSTOM-HEADER and its value is can be computed
options.Headers.Add("X-CUSTOM-HEADER", context =>
{
return new StringValues("SomeValue");
});
});
services.AddHttpClient<GitHubApionsumer>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");
// Github API versioning
c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
// Github requires a user-agent
c.DefaultRequestHeaders.Add("User-Agent", "theCodeBlogger.com Demo");
}).AddHeaderPropagation(); // Apply Propagation to Typed Client
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// Add middleware in request pipeline
app.UseHeaderPropagation();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
view raw Startup.cs hosted with ❤ by GitHub

I used an .NET Core MVC application to test this. As you can see, below snapshot shows two headers:

  • a new header X-CUSTOM-HEADER is inserted to the outgoing request.
  • Value from User-Agent incoming request header is used for new header X-RENAMED-HEADER
Incoming Request Header Propagation using Middleware
Incoming Request Header Propagation using Middleware

Have you ever came across this scenario where you had to forward the request headers from incoming request to outgoing request ? If yes, how did you implement it ? Let me know your thoughts.

Leave a Reply