@@ -17,7 +17,20 @@ public partial class Program
1717{
1818 private static void Main ( string [ ] args )
1919 {
20- WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
20+ WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
21+
22+ builder . Services . Configure < ForwardedHeadersOptions > ( options =>
23+ {
24+ options . ForwardedHeaders =
25+ ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto ;
26+
27+ // Only loopback proxies are allowed by default.
28+ // Clear that restriction because forwarders are enabled by explicit
29+ // configuration.
30+ options . KnownNetworks . Clear ( ) ;
31+ options . KnownProxies . Clear ( ) ;
32+ } ) ;
33+
2134 ConfigurationManager configuration = builder . Configuration ;
2235 string connectionString = builder . Configuration . GetConnectionString ( "EssentialCSharpWebContextConnection" ) ?? throw new InvalidOperationException ( "Connection string 'EssentialCSharpWebContextConnection' not found." ) ;
2336
@@ -126,38 +139,35 @@ private static void Main(string[] args)
126139 {
127140 microsoftoptions . ClientId = configuration [ "authentication:microsoft:clientid" ] ?? throw new InvalidOperationException ( "authentication:microsoft:clientid unexpectedly null" ) ;
128141 microsoftoptions . ClientSecret = configuration [ "authentication:microsoft:clientsecret" ] ?? throw new InvalidOperationException ( "authentication:microsoft:clientsecret unexpectedly null" ) ;
129- microsoftoptions . CallbackPath = "/signin-microsoft" ;
130142 } )
131143 . AddGitHub ( o =>
132144 {
133145 o . ClientId = configuration [ "authentication:github:clientId" ] ?? throw new InvalidOperationException ( "github:clientId unexpectedly null" ) ;
134146 o . ClientSecret = configuration [ "authentication:github:clientSecret" ] ?? throw new InvalidOperationException ( "github:clientSecret unexpectedly null" ) ;
135- o . CallbackPath = "/signin-github" ;
136147
137148 // Grants access to read a user's profile data.
138149 // https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps
139150 o . Scope . Add ( "read:user" ) ;
140151 } ) ;
141152 }
142153
143- builder . Services . Configure < ForwardedHeadersOptions > ( options =>
144- {
145- options . ForwardedHeaders =
146- ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto ;
147- } ) ;
148-
149- WebApplication app = builder . Build ( ) ;
150154
151- app . UseForwardedHeaders ( ) ;
152155
156+ WebApplication app = builder . Build ( ) ;
153157 // Configure the HTTP request pipeline.
154158 if ( ! app . Environment . IsDevelopment ( ) )
155159 {
156160 app . UseExceptionHandler ( "/Error" ) ;
161+ app . UseForwardedHeaders ( ) ;
157162 app . UseHsts ( ) ;
158163 app . UseSecurityHeadersMiddleware ( new SecurityHeadersBuilder ( )
159164 . AddDefaultSecurePolicy ( ) ) ;
160165 }
166+ else
167+ {
168+ app . UseDeveloperExceptionPage ( ) ;
169+ app . UseForwardedHeaders ( ) ;
170+ }
161171
162172 app . MapHealthChecks ( "/healthz" ) ;
163173
@@ -169,20 +179,12 @@ private static void Main(string[] args)
169179 app . UseAuthentication ( ) ;
170180 app . UseAuthorization ( ) ;
171181 app . UseMiddleware < ReferralMiddleware > ( ) ;
172-
173- app . Use ( ( context , next ) =>
174- {
175- context . Request . Scheme = "https" ;
176- return next ( context ) ;
177- } ) ;
178182
179- app . MapDefaultControllerRoute ( ) ;
180- app . MapRazorPages ( ) ;
181183
182- app . MapControllerRoute (
183- name : "slug" ,
184- pattern : "{*key}" ,
185- defaults : new { controller = "Home ", action = "Index" } ) ;
184+ app . MapRazorPages ( ) ;
185+ app . MapDefaultControllerRoute ( ) ;
186+
187+ app . MapFallbackToController ( "Index ", "Home" ) ;
186188
187189 app . Run ( ) ;
188190 }
0 commit comments