@@ -113,11 +113,19 @@ private async Task HandleResourceMetadataRequestAsync()
113113 }
114114
115115 /// <inheritdoc />
116- protected override Task < AuthenticateResult > HandleAuthenticateAsync ( )
116+ protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
117117 {
118- // This handler doesn't perform authentication - it only adds resource metadata to challenges
119- // The actual authentication will be handled by the bearer token handler or other authentication handlers
120- return Task . FromResult ( AuthenticateResult . NoResult ( ) ) ;
118+ // If ForwardAuthenticate is set, forward the authentication to the specified scheme
119+ if ( ! string . IsNullOrEmpty ( Options . ForwardAuthenticate ) &&
120+ Options . ForwardAuthenticate != Scheme . Name )
121+ {
122+ // Simply forward the authentication request to the specified scheme and return its result
123+ // This ensures we don't interfere with the authentication process
124+ return await Context . AuthenticateAsync ( Options . ForwardAuthenticate ) ;
125+ }
126+
127+ // If no forwarding is configured, this handler doesn't perform authentication
128+ return AuthenticateResult . NoResult ( ) ;
121129 }
122130
123131 /// <inheritdoc />
@@ -135,24 +143,9 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties
135143 // Store the resource_metadata in properties in case other handlers need it
136144 properties . Items [ "resource_metadata" ] = rawPrmDocumentUri ;
137145
138- // Get supported schemes from the options
139- var options = _optionsMonitor . CurrentValue ;
140- var supportedSchemes = options . GetSupportedAuthenticationSchemes ( Request . HttpContext ) . ToList ( ) ;
141-
142- // If no schemes are explicitly defined, don't add any WWW-Authenticate headers
143- if ( supportedSchemes . Count == 0 )
144- {
145- return base . HandleChallengeAsync ( properties ) ;
146- }
147-
148- // Add headers for each supported authentication scheme
149- foreach ( var scheme in supportedSchemes )
150- {
151- // For all schemes, include the realm and resource metadata
152- // This allows discovery of OAuth capabilities regardless of the authentication scheme
153- string headerValue = $ "{ scheme } realm=\" { Scheme . Name } \" , resource_metadata=\" { rawPrmDocumentUri } \" ";
154- Response . Headers . Append ( "WWW-Authenticate" , headerValue ) ;
155- }
146+ // Add the WWW-Authenticate header with Bearer scheme and resource metadata
147+ string headerValue = $ "Bearer realm=\" { Scheme . Name } \" , resource_metadata=\" { rawPrmDocumentUri } \" ";
148+ Response . Headers . Append ( "WWW-Authenticate" , headerValue ) ;
156149
157150 return base . HandleChallengeAsync ( properties ) ;
158151 }
0 commit comments