File tree Expand file tree Collapse file tree 3 files changed +49
-14
lines changed Expand file tree Collapse file tree 3 files changed +49
-14
lines changed Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using Microsoft . AspNetCore . Builder ;
5+ using Microsoft . AspNetCore . HttpOverrides ;
6+ using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . Options ;
8+
9+ namespace Microsoft . AspNetCore
10+ {
11+ internal class ForwardedHeadersOptionsSetup : IConfigureOptions < ForwardedHeadersOptions >
12+ {
13+ private readonly IConfiguration _configuration ;
14+
15+ public ForwardedHeadersOptionsSetup ( IConfiguration configuration )
16+ {
17+ _configuration = configuration ;
18+ }
19+
20+ public void Configure ( ForwardedHeadersOptions options )
21+ {
22+ if ( ! string . Equals ( "true" , _configuration [ "ForwardedHeaders_Enabled" ] , StringComparison . OrdinalIgnoreCase ) )
23+ {
24+ return ;
25+ }
26+
27+ options . ForwardedHeaders = ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto ;
28+ // Only loopback proxies are allowed by default. Clear that restriction because forwarders are
29+ // being enabled by explicit configuration.
30+ options . KnownNetworks . Clear ( ) ;
31+ options . KnownProxies . Clear ( ) ;
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 44using System ;
55using Microsoft . AspNetCore . Builder ;
66using Microsoft . AspNetCore . Hosting ;
7+ using Microsoft . Extensions . Configuration ;
78
89namespace Microsoft . AspNetCore
910{
1011 internal class ForwardedHeadersStartupFilter : IStartupFilter
1112 {
13+ private readonly IConfiguration _configuration ;
14+
15+ public ForwardedHeadersStartupFilter ( IConfiguration configuration )
16+ {
17+ _configuration = configuration ;
18+ }
19+
1220 public Action < IApplicationBuilder > Configure ( Action < IApplicationBuilder > next )
1321 {
22+ if ( ! string . Equals ( "true" , _configuration [ "ForwardedHeaders_Enabled" ] , StringComparison . OrdinalIgnoreCase ) )
23+ {
24+ return next ;
25+ }
26+
1427 return app =>
1528 {
1629 app . UseForwardedHeaders ( ) ;
Original file line number Diff line number Diff line change @@ -243,20 +243,8 @@ internal static void ConfigureWebDefaults(IWebHostBuilder builder)
243243 new ConfigurationChangeTokenSource < HostFilteringOptions > ( hostingContext . Configuration ) ) ;
244244
245245 services . AddTransient < IStartupFilter , HostFilteringStartupFilter > ( ) ;
246-
247- if ( string . Equals ( "true" , hostingContext . Configuration [ "ForwardedHeaders_Enabled" ] , StringComparison . OrdinalIgnoreCase ) )
248- {
249- services . Configure < ForwardedHeadersOptions > ( options =>
250- {
251- options . ForwardedHeaders = ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto ;
252- // Only loopback proxies are allowed by default. Clear that restriction because forwarders are
253- // being enabled by explicit configuration.
254- options . KnownNetworks . Clear ( ) ;
255- options . KnownProxies . Clear ( ) ;
256- } ) ;
257-
258- services . AddTransient < IStartupFilter , ForwardedHeadersStartupFilter > ( ) ;
259- }
246+ services . AddTransient < IStartupFilter , ForwardedHeadersStartupFilter > ( ) ;
247+ services . AddTransient < IConfigureOptions < ForwardedHeadersOptions > , ForwardedHeadersOptionsSetup > ( ) ;
260248
261249 services . AddRouting ( ) ;
262250 } )
You can’t perform that action at this time.
0 commit comments