Skip to content

Instantly share code, notes, and snippets.

@jglozano
Created August 4, 2022 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jglozano/879c107c63dc2128db8ccabf253a9acb to your computer and use it in GitHub Desktop.
Save jglozano/879c107c63dc2128db8ccabf253a9acb to your computer and use it in GitHub Desktop.
ASP.NET Core X-FORWARDED- Headers
using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Configure the Forwarded Headers
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
// Do the -For, -Proto, and -Host X-FORWARDED-*
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
});
var app = builder.Build();
// Add the middleware to handle the setting of
//
// HttpContext.Connection.RemoteIpAddress: Set using the X-Forwarded-For header value.
// HttpContext.Request.Scheme: Set using the X-Forwarded - Proto header value.
// HttpContext.Request.Host: Set using the X-Forwarded - Host header value.
//
app.UseForwardedHeaders();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment