无法从特定角色中删除用户

Nei*_*ith 5 c# asp.net asp.net-web-api asp.net-identity

使用web-api 2和identity 2,我正在尝试使用用户ID和角色名创建一个从角色中删除用户的操作.我正在使用nuget identity2示例提供的ApplicationUserManager.

我的行动

[HttpPost]
[Route("RemoveUserFromRole")]
public async Task<IHttpActionResult> RemoveUserFromRole(UserRolesViewModel model) 
{
    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    var result = await UserManager.RemoveUserFromRolesAsync(
        model.UserId, model.RoleNames);

    if (result.Errors.Any())
        return InternalServerError(); 

    return Ok();
}
Run Code Online (Sandbox Code Playgroud)

我的观点模型:

public class UserRolesViewModel
{
    [Required]
    public string UserId { get; set; }

    [Required]
    public IList<string> RoleNames { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ApplicationUserManager的RemoveUserFromRolesAsync:

public virtual async Task<IdentityResult> RemoveUserFromRolesAsync(
    string userId, IList<string> roles) 
{
    var userRoleStore = (IUserRoleStore<ApplicationUser, string>) Store;

    var user = await FindByIdAsync(userId).ConfigureAwait(false);
    if (user == null)
        throw new InvalidOperationException("Invalid user Id");

    var userRoles = await userRoleStore.GetRolesAsync(user).ConfigureAwait(false);
    foreach (var role in roles.Where(userRoles.Contains))
        await userRoleStore.RemoveFromRoleAsync(user, role).ConfigureAwait(false);

    return await UpdateAsync(user).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果用户属于角色'User'和'Mod',则无法从'Mod'中删除用户.发布以下json会将用户从"User"角色中删除,如预期:

{
    "userId": "0d5f97e4-65a0-43ad-b889-0af98a7ff326",
    "roleNames": [
        "User"
    ]
}
Run Code Online (Sandbox Code Playgroud)

但是给定以下json,用户不会从'Mod'中删除,而是从'User'中删除:

{
    "userId": "0d5f97e4-65a0-43ad-b889-0af98a7ff326",
    "roleNames": [
        "Mod"
    ]
}
Run Code Online (Sandbox Code Playgroud)

调试显示,当给定角色'Mod'时,正确的用户ID和角色名称将传递到userRoleStore.

Hao*_*ung 4

这是一个应该在 2.0.1 版本中修复的错误