@@ -98,7 +98,7 @@ public TokenAcquisition(IConfiguration configuration, IMSALAppTokenCacheProvider
98
98
/// From the configuration of the Authentication of the ASP.NET Core Web API:
99
99
/// <code>OpenIdConnectOptions options;</code>
100
100
///
101
- /// Subscribe to the authorization code recieved event:
101
+ /// Subscribe to the authorization code received event:
102
102
/// <code>
103
103
/// options.Events = new OpenIdConnectEvents();
104
104
/// options.Events.OnAuthorizationCodeReceived = OnAuthorizationCodeReceived;
@@ -125,9 +125,17 @@ public async Task AddAccountToCacheFromAuthorizationCode(AuthorizationCodeReceiv
125
125
try
126
126
{
127
127
// 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")
129
130
context . HandleCodeRedemption ( ) ;
130
131
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
+
131
139
var application = GetOrBuildConfidentialClientApplication ( context . HttpContext , context . Principal ) ;
132
140
133
141
// 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)
272
280
account = accounts . FirstOrDefault ( a => a . Username == user . GetLoginHint ( ) ) ;
273
281
}
274
282
275
- if ( account != null )
283
+ if ( account != null )
276
284
{
277
285
this . UserTokenCacheProvider ? . Clear ( account . HomeAccountId . Identifier ) ;
278
286
279
287
await app . RemoveAsync ( account ) ;
280
288
}
281
289
}
282
290
283
- IConfidentialClientApplication application ;
291
+ private IConfidentialClientApplication application ;
284
292
285
293
/// <summary>
286
294
/// Creates an MSAL Confidential client application if needed
@@ -359,14 +367,15 @@ private async Task<string> GetAccessTokenOnBehalfOfUser(IConfidentialClientAppli
359
367
// Get the account
360
368
IAccount account = await application . GetAccountAsync ( accountIdentifier ) ;
361
369
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.
363
371
if ( account == null )
364
372
{
365
373
var accounts = await application . GetAccountsAsync ( ) ;
366
374
account = accounts . FirstOrDefault ( a => a . Username == loginHint ) ;
367
375
}
368
376
369
- AuthenticationResult result ;
377
+ AuthenticationResult result = null ;
378
+
370
379
if ( string . IsNullOrWhiteSpace ( tenant ) )
371
380
{
372
381
result = await application . AcquireTokenSilent ( scopes . Except ( scopesRequestedByMsalNet ) , account )
@@ -379,6 +388,7 @@ private async Task<string> GetAccessTokenOnBehalfOfUser(IConfidentialClientAppli
379
388
. WithAuthority ( authority )
380
389
. ExecuteAsync ( ) ;
381
390
}
391
+
382
392
return result . AccessToken ;
383
393
}
384
394
@@ -417,9 +427,8 @@ private void AddAccountToCacheFromJwt(IEnumerable<string> scopes, JwtSecurityTok
417
427
}
418
428
}
419
429
420
-
421
430
/// <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).
423
432
/// Replies to the client through the HttpReponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
424
433
/// the client can trigger an iteraction with the user so that the user consents to more scopes
425
434
/// </summary>
@@ -466,7 +475,7 @@ private static bool AcceptedTokenVersionIsNotTheSameAsTokenVersion(MsalUiRequire
466
475
{
467
476
// Normally app developers should not make decisions based on the internal AAD code
468
477
// 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.
470
479
// This is subject to change in the future
471
480
return ( msalSeviceException . Message . Contains ( "AADSTS50013" ) ) ;
472
481
}
0 commit comments