Skip to content

AspNetHeaderReplicator is a lightweight ASP.NET library that enables seamless replication of HTTP request headers to response headers, based on customizable allow and discard rules.

License

Notifications You must be signed in to change notification settings

btungut/AspNetHeaderReplicator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Actions Workflow Status Code Coverage GitHub Release NuGet Version

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 --version 0.9.1

Or with the nuget command:

nuget install AspNetHeaderReplicator -Version 0.9.1

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");
    });
}



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.




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...

About

AspNetHeaderReplicator is a lightweight ASP.NET library that enables seamless replication of HTTP request headers to response headers, based on customizable allow and discard rules.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published