UserManager RemoveFromRole bug

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?

Upvotes: 4

Views: 1882

Answers (3)

Marcel Melzig
Marcel Melzig

Reputation: 363

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;
}

Upvotes: 0

dafriskymonkey
dafriskymonkey

Reputation: 2189

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

Upvotes: 1

pranav rastogi
pranav rastogi

Reputation: 4144

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

Upvotes: 2

Related Questions