66using System . Diagnostics . CodeAnalysis ;
77using System . Linq ;
88using System . Security . Claims ;
9+ using System . Threading . Tasks ;
910using Microsoft . AspNetCore . Authentication . OpenIdConnect ;
1011using Microsoft . Extensions . Configuration ;
1112using Microsoft . Extensions . DependencyInjection ;
@@ -79,7 +80,7 @@ public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder EnableTokenAcquisiti
7980 WebAppCallsWebApiImplementation (
8081 Services ,
8182 initialScopes ,
82- ConfigureMicrosoftIdentityOptions ,
83+ null , /* to avoid calling the delegate twice */
8384 OpenIdConnectScheme ,
8485 configureConfidentialClientApplicationOptions ) ;
8586 return new MicrosoftIdentityAppCallsWebApiAuthenticationBuilder (
@@ -93,14 +94,21 @@ public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder EnableTokenAcquisiti
9394 internal static void WebAppCallsWebApiImplementation (
9495 IServiceCollection services ,
9596 IEnumerable < string > ? initialScopes ,
96- Action < MicrosoftIdentityOptions > configureMicrosoftIdentityOptions ,
97+ Action < MicrosoftIdentityOptions > ? configureMicrosoftIdentityOptions ,
9798 string openIdConnectScheme ,
9899 Action < ConfidentialClientApplicationOptions > ? configureConfidentialClientApplicationOptions )
99100 {
100- // Ensure that configuration options for MSAL.NET, HttpContext accessor and the Token acquisition service
101- // (encapsulating MSAL.NET) are available through dependency injection
102- services . Configure ( openIdConnectScheme , configureMicrosoftIdentityOptions ) ;
103-
101+ // When called from MISE, ensure that configuration options for MSAL.NET, HttpContext accessor
102+ // and the Token acquisition service (encapsulating MSAL.NET) are available through dependency injection.
103+ // When called from AddMicrosoftIdentityWebApp(delegate), should not be re-configured otherwise
104+ // the delegate would be called twice.
105+ if ( configureMicrosoftIdentityOptions != null )
106+ {
107+ // Won't be null in the case where the caller is MISE (to ensure that the configuration for MSAL.NET
108+ // is available through DI).
109+ // Will be null when called from AddMicrosoftIdentityWebApp(delegate) to avoid calling the delegate twice.
110+ services . Configure ( openIdConnectScheme , configureMicrosoftIdentityOptions ) ;
111+ }
104112 if ( configureConfidentialClientApplicationOptions != null )
105113 {
106114 services . Configure ( openIdConnectScheme , configureConfidentialClientApplicationOptions ) ;
@@ -157,8 +165,7 @@ internal static void WebAppCallsWebApiImplementation(
157165 } ;
158166
159167 // Handling the token validated to get the client_info for cases where tenantId is not present (example: B2C)
160- var onTokenValidatedHandler = options . Events . OnTokenValidated ;
161- options . Events . OnTokenValidated = async context =>
168+ options . Events . OnTokenValidated += async context =>
162169 {
163170 string ? clientInfo = context ! . ProtocolMessage ? . GetParameter ( ClaimConstants . ClientInfo ) ;
164171
@@ -172,8 +179,7 @@ internal static void WebAppCallsWebApiImplementation(
172179 context ! . Principal ! . Identities . FirstOrDefault ( ) ? . AddClaim ( new Claim ( ClaimConstants . UniqueObjectIdentifier , clientInfoFromServer . UniqueObjectIdentifier ) ) ;
173180 }
174181 }
175-
176- await onTokenValidatedHandler ( context ) . ConfigureAwait ( false ) ;
182+ await Task . CompletedTask ;
177183 } ;
178184
179185 // Handling the sign-out: removing the account from MSAL.NET cache
0 commit comments