File tree Expand file tree Collapse file tree 5 files changed +21
-33
lines changed
Expand file tree Collapse file tree 5 files changed +21
-33
lines changed Original file line number Diff line number Diff line change @@ -125,9 +125,18 @@ 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+
139+
131140 var application = GetOrBuildConfidentialClientApplication ( context . HttpContext , context . Principal ) ;
132141
133142 // 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,7 +281,7 @@ public async Task RemoveAccount(RedirectContext context)
272281 account = accounts . FirstOrDefault ( a => a . Username == user . GetLoginHint ( ) ) ;
273282 }
274283
275- if ( account != null )
284+ if ( account != null )
276285 {
277286 this . UserTokenCacheProvider ? . Clear ( account . HomeAccountId . Identifier ) ;
278287
Original file line number Diff line number Diff line change @@ -94,11 +94,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
9494 // if the access operation resulted in a cache update
9595 if ( args . HasStateChanged )
9696 {
97- string cacheKey = args . Account ? . HomeAccountId ? . Identifier ;
98- if ( string . IsNullOrEmpty ( cacheKey ) )
99- {
100- cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
101- }
97+ string cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
10298
10399 if ( string . IsNullOrWhiteSpace ( cacheKey ) )
104100 return ;
@@ -116,17 +112,13 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
116112 /// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
117113 private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
118114 {
119- string cacheKey = args . Account ? . HomeAccountId ? . Identifier ;
120- if ( string . IsNullOrEmpty ( cacheKey ) )
121- {
122- cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
123- }
115+ string cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
124116
125117 if ( string . IsNullOrWhiteSpace ( cacheKey ) )
126118 return ;
127119
128120 byte [ ] tokenCacheBytes = ( byte [ ] ) this . memoryCache . Get ( cacheKey ) ;
129- args . TokenCache . DeserializeMsalV3 ( tokenCacheBytes , shouldClearExistingCache : true ) ;
121+ args . TokenCache . DeserializeMsalV3 ( tokenCacheBytes , shouldClearExistingCache : true ) ;
130122 }
131123
132124 /// <summary>
Original file line number Diff line number Diff line change @@ -107,11 +107,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
107107 // if the access operation resulted in a cache update
108108 if ( args . HasStateChanged )
109109 {
110- string cacheKey = args . Account ? . HomeAccountId ? . Identifier ;
111- if ( string . IsNullOrEmpty ( cacheKey ) )
112- {
113- cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
114- }
110+ string cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
115111
116112 if ( string . IsNullOrWhiteSpace ( cacheKey ) )
117113 return ;
@@ -140,11 +136,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
140136 private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
141137 {
142138 this . HttpContext . Session . LoadAsync ( ) . Wait ( ) ;
143- string cacheKey = args . Account ? . HomeAccountId ? . Identifier ;
144- if ( string . IsNullOrEmpty ( cacheKey ) )
145- {
146- cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
147- }
139+ string cacheKey = httpContextAccessor . HttpContext . User . GetMsalAccountId ( ) ;
148140 if ( string . IsNullOrWhiteSpace ( cacheKey ) )
149141 return ;
150142
Original file line number Diff line number Diff line change @@ -121,11 +121,7 @@ private void UserTokenCacheBeforeAccessNotification(TokenCacheNotificationArgs a
121121 /// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
122122 private void UserTokenCacheAfterAccessNotification ( TokenCacheNotificationArgs args )
123123 {
124- string accountId = args . Account ? . HomeAccountId ? . Identifier ;
125- if ( string . IsNullOrEmpty ( accountId ) )
126- {
127- accountId = httpContextAccesssor . HttpContext . User . GetMsalAccountId ( ) ;
128- }
124+ string accountId = httpContextAccesssor . HttpContext . User . GetMsalAccountId ( ) ;
129125
130126 // if state changed, i.e. new token obtained
131127 if ( args . HasStateChanged && ! string . IsNullOrWhiteSpace ( accountId ) )
@@ -160,11 +156,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
160156 /// </summary>
161157 private void ReadCacheForSignedInUser ( TokenCacheNotificationArgs args )
162158 {
163- string accountId = args . Account ? . HomeAccountId ? . Identifier ;
164- if ( string . IsNullOrEmpty ( accountId ) )
165- {
166- accountId = httpContextAccesssor . HttpContext . User . GetMsalAccountId ( ) ;
167- }
159+ string accountId = httpContextAccesssor . HttpContext . User . GetMsalAccountId ( ) ;
168160 if ( this . InMemoryCache == null ) // first time access
169161 {
170162 this . InMemoryCache = GetLatestUserRecordQuery ( accountId ) . FirstOrDefault ( ) ;
Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ public static IServiceCollection AddAzureAdV2Authentication(this IServiceCollect
7676 // and [Access Tokens](https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens)
7777 options . TokenValidationParameters . NameClaimType = "preferred_username" ;
7878
79+ // Force the account selection (to avoid automatic sign-in with the account signed-in with Windows)
80+ //options.Prompt = "select_account";
81+
7982 // Handling the sign-out
8083 options . Events . OnRedirectToIdentityProviderForSignOut = async context =>
8184 {
You can’t perform that action at this time.
0 commit comments