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
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
+
139
+
131
140
var application = GetOrBuildConfidentialClientApplication ( context . HttpContext , context . Principal ) ;
132
141
133
142
// 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)
272
281
account = accounts . FirstOrDefault ( a => a . Username == user . GetLoginHint ( ) ) ;
273
282
}
274
283
275
- if ( account != null )
284
+ if ( account != null )
276
285
{
277
286
this . UserTokenCacheProvider ? . Clear ( account . HomeAccountId . Identifier ) ;
278
287
Original file line number Diff line number Diff line change @@ -94,11 +94,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
94
94
// if the access operation resulted in a cache update
95
95
if ( args . HasStateChanged )
96
96
{
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 ( ) ;
102
98
103
99
if ( string . IsNullOrWhiteSpace ( cacheKey ) )
104
100
return ;
@@ -116,17 +112,13 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
116
112
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
117
113
private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
118
114
{
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 ( ) ;
124
116
125
117
if ( string . IsNullOrWhiteSpace ( cacheKey ) )
126
118
return ;
127
119
128
120
byte [ ] tokenCacheBytes = ( byte [ ] ) this . memoryCache . Get ( cacheKey ) ;
129
- args . TokenCache . DeserializeMsalV3 ( tokenCacheBytes , shouldClearExistingCache : true ) ;
121
+ args . TokenCache . DeserializeMsalV3 ( tokenCacheBytes , shouldClearExistingCache : true ) ;
130
122
}
131
123
132
124
/// <summary>
Original file line number Diff line number Diff line change @@ -107,11 +107,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
107
107
// if the access operation resulted in a cache update
108
108
if ( args . HasStateChanged )
109
109
{
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 ( ) ;
115
111
116
112
if ( string . IsNullOrWhiteSpace ( cacheKey ) )
117
113
return ;
@@ -140,11 +136,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
140
136
private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
141
137
{
142
138
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 ( ) ;
148
140
if ( string . IsNullOrWhiteSpace ( cacheKey ) )
149
141
return ;
150
142
Original file line number Diff line number Diff line change @@ -121,11 +121,7 @@ private void UserTokenCacheBeforeAccessNotification(TokenCacheNotificationArgs a
121
121
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
122
122
private void UserTokenCacheAfterAccessNotification ( TokenCacheNotificationArgs args )
123
123
{
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 ( ) ;
129
125
130
126
// if state changed, i.e. new token obtained
131
127
if ( args . HasStateChanged && ! string . IsNullOrWhiteSpace ( accountId ) )
@@ -160,11 +156,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
160
156
/// </summary>
161
157
private void ReadCacheForSignedInUser ( TokenCacheNotificationArgs args )
162
158
{
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 ( ) ;
168
160
if ( this . InMemoryCache == null ) // first time access
169
161
{
170
162
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
76
76
// and [Access Tokens](https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens)
77
77
options . TokenValidationParameters . NameClaimType = "preferred_username" ;
78
78
79
+ // Force the account selection (to avoid automatic sign-in with the account signed-in with Windows)
80
+ //options.Prompt = "select_account";
81
+
79
82
// Handling the sign-out
80
83
options . Events . OnRedirectToIdentityProviderForSignOut = async context =>
81
84
{
You can’t perform that action at this time.
0 commit comments