Created
August 4, 2022 21:30
-
-
Save jglozano/879c107c63dc2128db8ccabf253a9acb to your computer and use it in GitHub Desktop.
ASP.NET Core X-FORWARDED- Headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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