4

The function (RemoveFromRole in UserManager) to remove user from role is seriously buggy! If users has many roles the function seems to remove a random role but fortunately correct user.

Has anyone else experienced this?

3 Answers3

2

There is a bug tracking this issue http://aspnetidentity.codeplex.com/workitem/2069 As a workaround you can remove all roles for the user and add the ones for now. It is not the best solution but it will work

pranav rastogi
  • 4,124
  • 23
  • 23
1

I experienced the exact same bug and it has been fixed in the 2.0.1. you only have to update.

dafriskymonkey
  • 2,189
  • 6
  • 25
  • 48
  • weird !! its worked for me. you can try to remove all roles then add the roles you want, since the add role function works without bugs. – dafriskymonkey Jul 17 '14 at 07:49
  • The bug is still there in Jan 2016. I have the same issues. Removing one or several roles seems to generate rather random outcomes. Even though the IdentityResult returned for each remove indicates success, the role reference is not removed from the db and an IsInRole test after will return true. – Johncl Jan 06 '16 at 12:01
0

I have encountered the bug with the method RemoveFromRoles. The error happend if i try to remove a role in which the user isn't. I had to write a method to get the names of the roles the user is in. That way i could only remove the user from those roles.

public List<string> GetRoleNamesOf(IdentityUser user)
{
    var roles = user.Roles;
    List<string> roleNames = new List<string>();

    foreach (var role in roles)
    {
        var roleName = this.FindById(role.RoleId).Name;
        roleNames.Add(roleName);
    }

    return roleNames;
}
Marcel Melzig
  • 363
  • 6
  • 13