@@ -98,7 +98,7 @@ public TokenAcquisition(IConfiguration configuration, IMSALAppTokenCacheProvider
9898 /// From the configuration of the Authentication of the ASP.NET Core Web API:
9999 /// <code>OpenIdConnectOptions options;</code>
100100 ///
101- /// Subscribe to the authorization code recieved event:
101+ /// Subscribe to the authorization code received event:
102102 /// <code>
103103 /// options.Events = new OpenIdConnectEvents();
104104 /// options.Events.OnAuthorizationCodeReceived = OnAuthorizationCodeReceived;
@@ -125,9 +125,17 @@ public async Task AddAccountToCacheFromAuthorizationCode(AuthorizationCodeReceiv
125125 try
126126 {
127127 // As AcquireTokenByAuthorizationCodeAsync is asynchronous we want to tell ASP.NET core that we are handing the code
128- // even if it's not done yet, so that it does not concurrently call the Token endpoint.
128+ // even if it's not done yet, so that it does not concurrently call the Token endpoint. (otherwise there will be a
129+ // race condition ending-up in an error from Azure AD telling "code already redeemed")
129130 context . HandleCodeRedemption ( ) ;
130131
132+ // The cache will need the claims from the ID token. In the case of guest scenarios
133+ // If they are not yet in the HttpContext.User's claims, adding them.
134+ if ( ! context . HttpContext . User . Claims . Any ( ) )
135+ {
136+ ( context . HttpContext . User . Identity as ClaimsIdentity ) . AddClaims ( context . Principal . Claims ) ;
137+ }
138+
131139 var application = GetOrBuildConfidentialClientApplication ( context . HttpContext , context . Principal ) ;
132140
133141 // Do not share the access token with ASP.NET Core otherwise ASP.NET will cache it and will not send the OAuth 2.0 request in
@@ -272,15 +280,15 @@ public async Task RemoveAccount(RedirectContext context)
272280 account = accounts . FirstOrDefault ( a => a . Username == user . GetLoginHint ( ) ) ;
273281 }
274282
275- if ( account != null )
283+ if ( account != null )
276284 {
277285 this . UserTokenCacheProvider ? . Clear ( account . HomeAccountId . Identifier ) ;
278286
279287 await app . RemoveAsync ( account ) ;
280288 }
281289 }
282290
283- IConfidentialClientApplication application ;
291+ private IConfidentialClientApplication application ;
284292
285293 /// <summary>
286294 /// Creates an MSAL Confidential client application if needed
@@ -359,14 +367,15 @@ private async Task<string> GetAccessTokenOnBehalfOfUser(IConfidentialClientAppli
359367 // Get the account
360368 IAccount account = await application . GetAccountAsync ( accountIdentifier ) ;
361369
362- // Special case for guest users as the Guest iod / tenant id are not surfaced.
370+ // Special case for guest users as the Guest id / tenant id are not surfaced.
363371 if ( account == null )
364372 {
365373 var accounts = await application . GetAccountsAsync ( ) ;
366374 account = accounts . FirstOrDefault ( a => a . Username == loginHint ) ;
367375 }
368376
369- AuthenticationResult result ;
377+ AuthenticationResult result = null ;
378+
370379 if ( string . IsNullOrWhiteSpace ( tenant ) )
371380 {
372381 result = await application . AcquireTokenSilent ( scopes . Except ( scopesRequestedByMsalNet ) , account )
@@ -379,6 +388,7 @@ private async Task<string> GetAccessTokenOnBehalfOfUser(IConfidentialClientAppli
379388 . WithAuthority ( authority )
380389 . ExecuteAsync ( ) ;
381390 }
391+
382392 return result . AccessToken ;
383393 }
384394
@@ -417,9 +427,8 @@ private void AddAccountToCacheFromJwt(IEnumerable<string> scopes, JwtSecurityTok
417427 }
418428 }
419429
420-
421430 /// <summary>
422- /// Used in Web APIs (which therefore cannot have an interaction with the user).
431+ /// Used in Web APIs (which therefore cannot have an interaction with the user).
423432 /// Replies to the client through the HttpReponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
424433 /// the client can trigger an iteraction with the user so that the user consents to more scopes
425434 /// </summary>
@@ -466,7 +475,7 @@ private static bool AcceptedTokenVersionIsNotTheSameAsTokenVersion(MsalUiRequire
466475 {
467476 // Normally app developers should not make decisions based on the internal AAD code
468477 // however until the STS sends sub-error codes for this error, this is the only
469- // way to distinguish the case.
478+ // way to distinguish the case.
470479 // This is subject to change in the future
471480 return ( msalSeviceException . Message . Contains ( "AADSTS50013" ) ) ;
472481 }
0 commit comments