ForwardedHeadersMiddleware seems not to pick up original host from Azure App Gateway
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
I have an ASP.NET Core app running in Azure Container Apps, which is protected by Azure App Gateway. Users access the app via the url of the app gateway.
The documentation at https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer seems not to lead to a working setup where the `ForwardedHeadersMiddleware` understands the original host (app gateway hostname) that users used to send requests to the app. See https://github.com/MicrosoftDocs/azure-docs/issues/97027#issuecomment-1229143586 for a similar thread.
[The Application Gateway sends the original host name in the `X-Original-Host` header](https://learn.microsoft.com/en-us/azure/application-gateway/how-application-gateway-works#:~:text=X%2Doriginal%2Dhost%20header%20contains%20the%20original%20host%20header%20with%20which%20the%20request%20arrived). I see that the `ForwardedHeadersOptions` has [`OriginalHostHeaderName`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.forwardedheadersoptions.originalhostheadername#microsoft-aspnetcore-builder-forwardedheadersoptions-originalhostheadername) but [the `ForwardedHeadersMiddleware` seems to overwrite that value instead of using it](https://github.com/dotnet/aspnetcore/blob/16e32c0594a55bf68c9589a9416dd64b60376e3e/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs#L348).
In any case, the resulting effect I see is that links generated using the [`LinkGenerator`](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing.Abstractions/src/LinkGenerator.cs) do not have the hostname that users are using.
### Expected Behavior
- Understandable documentation on how to setup ASP.NET Core behind Application Gateway.
- LinkGenerator generating links that are correct for end users, with the application being aware of the original host (from app gateway) that users are using.
### Steps To Reproduce
I tried configuring my app according to the documentation:
- I [set `ASPNETCORE_FORWARDEDHEADERS_ENABLED` to `true`](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-8.0#forward-the-scheme-for-linux-and-non-iis-reverse-proxies) in the container app's environment variables
- I added `app.UseForwardedHeaders()` (default settings) in `Program.cs`
### Exceptions (if any)
_No response_
### .NET Version
8.0.401
### Anything else?
Output of `dotnet --info`:
```
.NET SDK:
Version: 8.0.401
Commit: 811edcc344
Workload version: 8.0.400-manifests.57f7c351
MSBuild version: 17.11.4+37eb419ad
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.401\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[android]
Installation Source: SDK 8.0.400, VS 17.12.35209.166
Manifest Version: 34.0.113/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.113\WorkloadManifest.json
Install Type: FileBased
[aspire]
Installation Source: SDK 8.0.400, VS 17.12.35209.166
Manifest Version: 8.2.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.0\WorkloadManifest.json
Install Type: FileBased
[ios]
Installation Source: SDK 8.0.400, VS 17.12.35209.166
Manifest Version: 17.5.8020/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.5.8020\WorkloadManifest.json
Install Type: FileBased
[maccatalyst]
Installation Source: SDK 8.0.400, VS 17.12.35209.166
Manifest Version: 17.5.8020/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.5.8020\WorkloadManifest.json
Install Type: FileBased
[maui-windows]
Installation Source: SDK 8.0.400, VS 17.12.35209.166
Manifest Version: 8.0.72/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.72\WorkloadManifest.json
Install Type: FileBased
Host:
Version: 9.0.0-preview.7.24405.7
Architecture: x64
Commit: static
.NET SDKs installed:
8.0.401 [C:\Program Files\dotnet\sdk]
9.0.100-preview.7.24407.12 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-preview.7.24406.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-preview.7.24405.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0-preview.7.24405.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
X:\source\ra\mobile-app-backend\global.json
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
```
Contributor guide
Assessment
This issue has not been assessed yet.