liveBook · Manning

ForwardedHeadersMiddleware

As explained in ASP.NET Core in Action, 3e

ForwardedHeadersMiddleware is ASP.NET Core middleware that applies proxy-forwarded HTTP headers to the request context.

ForwardedHeadersMiddleware applies headers added by a reverse proxy to the ASP.NET Core request context. It lets the application recognize the original client IP address and request scheme, even when the proxy communicates with the app over HTTP. section 27.3.2 section 28.4.2

The middleware commonly handles these deployment details:

  • Reads X-Forwarded-For and updates the request’s client-address information. section 27.3.2
  • Reads X-Forwarded-Proto and updates Request.Scheme so later middleware can recognize an originally HTTPS request as secure. section 27.3.2 section 28.4.2
  • Runs before UseHttpsRedirection or UseHsts so those components receive the corrected scheme. section 28.4.2
  • Enables automatically in a disabled state with WebApplicationBuilder, or activates through ASPNETCORE_FORWARDEDHEADERS_ENABLED=true. section 27.3.2 section 28.4.2

Configure forwarded headers before HTTPS handling

UseForwardedHeaders can be placed near the start of the pipeline and configured for the headers supplied by the proxy: ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto. Follow it with UseHttpsRedirection and UseRouting so both receive the corrected request details. section 27.3.2

IIS integration handles header forwarding automatically through its integration configuration. With NGINX, HAProxy, or another reverse proxy, enable the middleware explicitly or configure it manually. section 27.3.2 section 28.4.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains forwarded headers, activation, configuration, and ordering, but does not detail header trust or validation rules.
Examples
Demonstrates ForwardedHeadersOptions with X-Forwarded-For and X-Forwarded-Proto before HTTPS redirection and routing.
What it assumes
Assumes a reverse-proxy deployment and introduces proxy-forwarded headers, request schemes, and middleware ordering.
What you can do after
Operate/configure an ASP.NET Core pipeline to interpret proxy headers before scheme-dependent middleware runs.
How it connects
Connects reverse proxies, Request.Scheme, HTTPS redirection, HSTS, IIS integration, NGINX, and HAProxy.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.