@@ -85,24 +85,20 @@ private static void ConfigureCookiePolicy(IServiceCollection services)
8585
8686 private static void ConfigureAuthentication ( IServiceCollection services , IConfiguration configuration )
8787 {
88- services . AddAuthentication ( options =>
89- {
90- options . DefaultScheme = CookieAuthenticationDefaults . AuthenticationScheme ;
91- options . DefaultChallengeScheme = OAuthSchemeName ;
92- } )
93- . AddCookie ( options =>
94- {
95- options . AccessDeniedPath = "/Errors/Display/401" ;
96- options . Cookie . Name = CookieName ;
97- options . Cookie . HttpOnly = true ;
98- options . Cookie . IsEssential = true ;
99- options . ExpireTimeSpan = TimeSpan . FromDays ( CookieExpirationDays ) ;
100- options . LoginPath = "/Identity/Login" ;
101- options . ReturnUrlParameter = CookieAuthenticationDefaults . ReturnUrlParameter ;
102- options . SlidingExpiration = true ;
103- } )
88+ // Do not override Identity's default schemes — AddIdentity already sets:
89+ // DefaultAuthenticateScheme = Identity.Application
90+ // DefaultSignInScheme = Identity.External
91+ // DefaultChallengeScheme = Identity.Application (login page redirect)
92+ // The controller explicitly challenges "XtremeIdiots" when needed.
93+ services . AddAuthentication ( )
10494 . AddOAuth ( OAuthSchemeName , options =>
10595 {
96+ // Sign into the external cookie so SignInManager.GetExternalLoginInfoAsync() works
97+ options . SignInScheme = IdentityConstants . ExternalScheme ;
98+
99+ // Must be essential so cookie consent policy doesn't block the correlation cookie
100+ options . CorrelationCookie . IsEssential = true ;
101+
106102 options . ClientId = GetConfigurationValue ( configuration , AuthClientIdKey ) ?? throw new InvalidOperationException ( "OAuth client ID is required" ) ;
107103 options . ClientSecret = GetConfigurationValue ( configuration , AuthClientSecretKey ) ?? throw new InvalidOperationException ( "OAuth client secret is required" ) ;
108104
@@ -148,6 +144,22 @@ private static void ConfigureAuthentication(IServiceCollection services, IConfig
148144 }
149145 } ;
150146 } ) ;
147+
148+ // Configure Identity's application cookie (replaces the removed AddCookie call)
149+ services . ConfigureApplicationCookie ( options =>
150+ {
151+ options . AccessDeniedPath = "/Errors/Display/401" ;
152+ options . Cookie . Name = CookieName ;
153+ options . Cookie . HttpOnly = true ;
154+ options . Cookie . IsEssential = true ;
155+ options . ExpireTimeSpan = TimeSpan . FromDays ( CookieExpirationDays ) ;
156+ options . LoginPath = "/Identity/Login" ;
157+ options . ReturnUrlParameter = CookieAuthenticationDefaults . ReturnUrlParameter ;
158+ options . SlidingExpiration = true ;
159+ } ) ;
160+
161+ // Ensure the external cookie used during OAuth callback is not blocked by consent policy
162+ services . ConfigureExternalCookie ( options => options . Cookie . IsEssential = true ) ;
151163 }
152164
153165 private static void ConfigureDataProtection ( IServiceCollection services )
0 commit comments