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.
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
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):
By default, the middleware will replicate and ignore the following headers:
new[] { "X-", "My-", "Req-", "Trace-", "Debug-", "Verbose-" }
new[] { "auth", "credential", "token", "pass", "secret", "hash", "cert" };
public void ConfigureServices(IServiceCollection services)
{
services.AddHeaderReplicator();
}
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");
});
}
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");
});
}
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");
});
}
You can create a new API project and apply the following changes...
...
...
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.");
});
});
}
...
...
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'
As you can see, headers are being executing without case sensitivity.
Response headers will be like below:
As you can see, headers are being replicated and/or ignored according to the configuration.
This project is licensed under the MIT License - see the LICENSE file for details.
Owner : Burak Tungut
Any contributions are welcome! Please post your issues and pull requests to the repository.
Regards...