AspNetHeaderReplicator 0.9.1

dotnet add package AspNetHeaderReplicator --version 0.9.1                
NuGet\Install-Package AspNetHeaderReplicator -Version 0.9.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspNetHeaderReplicator" Version="0.9.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNetHeaderReplicator --version 0.9.1                
#r "nuget: AspNetHeaderReplicator, 0.9.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AspNetHeaderReplicator as a Cake Addin
#addin nuget:?package=AspNetHeaderReplicator&version=0.9.1

// Install AspNetHeaderReplicator as a Cake Tool
#tool nuget:?package=AspNetHeaderReplicator&version=0.9.1                

AspNetHeaderReplicator

This is a simple library that exposes a middleware for ASP.NET Core applications that replicates headers from the request to the response with the ability to include or exclude specific headers.

Installation

AspNetHeaderReplicator is available as a NuGet package. You can install it using the NuGet Package Manager Console:

dotnet add package AspNetHeaderReplicator

Or with the nuget command:

nuget install AspNetHeaderReplicator

Usage

To use the middleware, you need to add it to the pipeline in the Configure method of your Startup class (Or you can use without the Startup class if you are using the generic host):

Default configuration

By default, the middleware will replicate and ignore the following headers:

Replicated headers
new[] { "X-", "My-", "Req-", "Trace-", "Debug-", "Verbose-" }
Ignored headers
new[] { "auth", "credential", "token", "pass", "secret", "hash", "cert" };

Example 01: Use default configuration

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator();
}

Example 02: Use default configuration AND add custom allowed headers

Following code snippet will allow headers starting with Allowed-Prefix to be replicated.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Allow headers starting with
        opt.AllowHeaderPrefix("Allowed-Prefix");
    });
}

Example 03: Use default configuration AND add custom ignored headers

Following code snippet will ignore headers containing ignored in their name.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Ignore headers containing
        opt.IgnoreHeaderSentence("ignored");
    });
}

Example 04: Clear default configuration AND add custom allowed headers with custom ignored headers

Following code snippet will clear all default settings and allow headers starting with X- and My- and ignore headers containing auth and credential.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Clear all default settings
        opt.ClearAll();

        // Allow headers starting with
        opt.AllowHeaderPrefix("X-");
        opt.AllowHeaderPrefix("My-");

        // Ignore headers containing
        opt.IgnoreHeaderSentence("auth");
        opt.IgnoreHeaderSentence("credential");
    });
}

<br /><br />

Demo

You can create a new API project and apply the following changes...

Demo: Startup.cs

...
...
public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Clear all default settings
        opt.ClearAll();

        // Allow headers starting with
        opt.AllowHeaderPrefix("X-");
        opt.AllowHeaderPrefix("My-");

        // Ignore headers containing
        opt.IgnoreHeaderSentence("auth");
        opt.IgnoreHeaderSentence("credential");
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHeaderReplicator();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.Map("/", context =>
        {
            return context.Response.WriteAsync("Please inspect the headers of this response.");
        });
    });
}
...
...

Demo: Test the API

Run the API and make a request to the root URL. You can use curl or any other tool to inspect the headers of the response.

curl --location 'http://localhost:5278/' \
    --header 'X-Burak: Burak Tungut' \
    --header 'My-Some: 123' \
    --header 'Some-Auth-Key: somesecrets' \
    --header 'a-header-credential-demo: somesecrets'

request headers

As you can see, headers are being executing without case sensitivity.

Response headers will be like below:

response headers

As you can see, headers are being replicated and/or ignored according to the configuration.


<br /><br />

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Owner : Burak Tungut

Any contributions are welcome! Please post your issues and pull requests to the repository.

Regards...

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.1 41 1/25/2025
0.9.1-rc3501 8 1/24/2025