Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public static void AddCaptchaService(this IServiceCollection services, IConfigur

public static void AddTrustedForwardedHeaders(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
{
// When ASPNETCORE_FORWARDEDHEADERS_ENABLED=true (recommended for Azure Container Apps),
// ASP.NET Core's built-in startup filter handles ForwardedHeaders with all proxies trusted.
// Skip manual configuration to avoid a conflicting no-trusted-proxies throw on startup.
if (string.Equals(configuration["ASPNETCORE_FORWARDEDHEADERS_ENABLED"], "true", StringComparison.OrdinalIgnoreCase))
return;

services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
Expand All @@ -37,7 +43,8 @@ public static void AddTrustedForwardedHeaders(this IServiceCollection services,
{
throw new InvalidOperationException(
"Forwarded headers are enabled but no trusted proxies are configured. " +
"Set ForwardedHeaders:TrustedProxyCidrs or ForwardedHeaders:TrustedProxies.");
"Set ForwardedHeaders:TrustedProxyCidrs or ForwardedHeaders:TrustedProxies, " +
"or set ASPNETCORE_FORWARDEDHEADERS_ENABLED=true for platform-managed proxies (e.g. Azure Container Apps).");
}
return;
}
Expand Down
12 changes: 10 additions & 2 deletions EssentialCSharp.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,12 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
}
});
});
app.UseForwardedHeaders();
// Skip manual UseForwardedHeaders when ASPNETCORE_FORWARDEDHEADERS_ENABLED=true;
// the built-in startup filter already called it before this pipeline runs.
if (!string.Equals(app.Configuration["ASPNETCORE_FORWARDEDHEADERS_ENABLED"], "true", StringComparison.OrdinalIgnoreCase))
{
app.UseForwardedHeaders();
}
Comment on lines +484 to +489

// Build dynamic CSP — TryDotNet origin comes from runtime config
string? tryDotNetOrigin = app.Configuration["TryDotNet:Origin"];
Expand Down Expand Up @@ -532,7 +537,10 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
else
{
app.UseDeveloperExceptionPage();
app.UseForwardedHeaders();
if (!string.Equals(app.Configuration["ASPNETCORE_FORWARDEDHEADERS_ENABLED"], "true", StringComparison.OrdinalIgnoreCase))
{
app.UseForwardedHeaders();
}
}

app.MapHealthChecks("/health").DisableRateLimiting();
Expand Down
Loading