Skip to content

Commit e3d9b04

Browse files
committed
Avoids collection was modified issue when flowing identities to the authenticated user's principal. (#18527)
1 parent 5b54bed commit e3d9b04

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Umbraco.Web.Common/Extensions/HttpContextExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ public static async Task<AuthenticateResult> AuthenticateBackOfficeAsync(this Ht
6868
// Otherwise we can't log in as both a member and a backoffice user
6969
// For instance if you've enabled basic auth.
7070
ClaimsPrincipal? authenticatedPrincipal = result.Principal;
71-
IEnumerable<ClaimsIdentity> existingIdentities = httpContext.User.Identities.Where(x => x.IsAuthenticated && x.AuthenticationType != authenticatedPrincipal.Identity.AuthenticationType);
71+
72+
// Make sure to copy into a list before attempting to update the authenticated principal, so we don't attempt to modify
73+
// the collection while iterating it.
74+
// See: https://github.com/umbraco/Umbraco-CMS/issues/18509
75+
var existingIdentities = httpContext.User.Identities
76+
.Where(x => x.IsAuthenticated && x.AuthenticationType != authenticatedPrincipal.Identity.AuthenticationType)
77+
.ToList();
7278
authenticatedPrincipal.AddIdentities(existingIdentities);
7379

7480
httpContext.User = authenticatedPrincipal;

0 commit comments

Comments
 (0)