dotnet/aspnetcore

UserManager.AddToRolesAsync() and RemoveFromRolesAsync() fail on any conflict

CollectorOfBolts opened this issue · 0 comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When adding multiple roles or generally updating a user's roles, UserManager.AddToRolesAsync() will fail upon finding that the user already has one of the given roles. Similary, UserManager.RemoveFromRolesAsync() will fail if the user does not have any of the given roles to be removed.

Expected Behavior

UserManager.AddToRolesAsync() should add all given roles to the user and simply skip any that the user already has and UserManager.RemoveFromRolesAsync() should remove all given roles from the user and simply skip any that the user does not have.

Steps To Reproduce

// with userManager coming from dependency injection and user being the IdentityUser object for some user
string role1 = "Role1";
await userManager.AddToRoleAsync(user, role1); // success
string[] roleGroup = [ "Role1", "Role2", "Role3" ];
IdentityResult result = await userManager.AddToRolesAsync(user, roleGroup);
if (!result.Succeeded) { throw new Exception("Failed to add roles"); } // will always throw here

Exceptions (if any)

No response

.NET Version

8.0.101

Anything else?

No response