简体   繁体   English

AspNetCore.Identity RemoveFromRoleAsync 不会从用户中删除角色

[英]AspNetCore.Identity RemoveFromRoleAsync does not remove role from user

I'm using ASP.NET Core Identity 2.0.2.我正在使用 ASP.NET Core Identity 2.0.2。 When I call UserManager.RemoveFromRoleAsync , it does not remove the role from the user.当我调用UserManager.RemoveFromRoleAsync时,它不会从用户中删除角色。 It says:它说:

Microsoft.AspNetCore.Identity.UserManager: Warning: User b651a459-5d6b-4239-88e6-facb33f11e87 is not in role Administrator. Microsoft.AspNetCore.Identity.UserManager:警告:用户 b651a459-5d6b-4239-88e6-facb33f11e87 不是管理员角色。

Here's the code:这是代码:

var userResult = await _userManager.RemoveFromRoleAsync(
    user,
    requestRole.ToReadableString());

I also tried to pass the role as an array in the RemoveFromRolesAsync function to no avail.我还尝试在RemoveFromRolesAsync function 中将角色作为数组传递,但无济于事。

I don't see any other way to remove a role from a user.我看不到任何其他方法可以从用户中删除角色。


Update更新

var roleResult = await _userManager
    .IsInRoleAsync(
        user,
        requestRole.ToReadableString());

var roleResult2 = await _userManager
    .IsInRoleAsync(
        user,
        "Administrator");

var roles = await _userManager
    .GetRolesAsync(user);

var userResult = await _userManager
    .RemoveFromRoleAsync(
        user,
        requestRole.ToReadableString());

roleResult and roleResult2 are both false. roleResult 和 roleResult2 都是假的。
roles contains "Administrator".角色包含“管理员”。
the user has the Role "Administrator" if I login with this user.如果我使用此用户登录,则该用户具有“管理员”角色。
So the Authorize Attribute says the user has this role:所以授权属性说用户有这个角色:

[Authorize(Roles = "Administrator", AuthenticationSchemes = "Bearer")]

Antoher test:另一个测试:

var roles = await _userManager
    .GetRolesAsync(user);

foreach (string role in roles)
{
    var roleTempResult = await _userManager
        .IsInRoleAsync(
            user,
            role);
}

The method IsInRoleAsync() returs false for all the roles returned from GetRolesAsync(user);对于从 GetRolesAsync(user) 返回的所有角色,IsInRoleAsync() 方法返回 false;
Whats going on there?那里发生了什么事?

Please

  1. Look a correct name of role in "AspNetRoles" table 在“ AspNetRoles”表中查找正确的角色名称

  2. Look is user in role in "AspNetUserRoles" table 看起来是“ AspNetUserRoles”表中的角色用户

  3. Specify a role name from "AspNetRoles" in RemoveFromRoleAsync 从RemoveFromRoleAsync中的“ AspNetRoles”中指定角色名称

You must use 'RoleName' instead of 'RoleId' in RemoveFromRoleAsync method.您必须在 RemoveFromRoleAsync 方法中使用“RoleName”而不是“RoleId”。

await _userManager.RemoveFromRoleAsync(user,"Administrator");

I hit this exact problem as well.我也遇到了这个确切的问题。

The problem was that the user I was passing in to all these functions was not a complete IdentityUser.问题是我传递给所有这些函数的用户不是一个完整的 IdentityUser。 It was an instance of IdentityUser and had the correct Id, UserName and Email, but other values like NormalizedEmail, NormalizedUserName and PasswordHash had been discarded when the user was retrieved in an earlier process.它是 IdentityUser 的一个实例,具有正确的 Id、UserName 和 Email,但在之前的过程中检索用户时,其他值(如 NormalizedEmail、NormalizedUserName 和 PasswordHash)已被丢弃。

Once I changed the retrieve process to not transform the users returned from GetUsersInRoleAsync, (I'd been populating a list with new IdentityUser instances based on the returned users and only setting the values I needed. Changing this to populate the list with the actual returned users fixed the problems), IsInRoleAsync and RemoveFromRoleAsync worked as expected.一旦我将检索过程更改为不转换从 GetUsersInRoleAsync 返回的用户,(我一直在根据返回的用户使用新的 IdentityUser 实例填充一个列表,并且只设置我需要的值。更改它以使用实际返回的填充列表用户修复了问题),IsInRoleAsync 和 RemoveFromRoleAsync 按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewComponent 无法识别 AspNetCore.Identity“用户” - ViewComponent does not recognize AspNetCore.Identity "User" AspNetCore.Identity 不适用于自定义用户/角色实现 - AspNetCore.Identity not working with custom User/Role implementation AspNetCore.Identity - 如何为新用户设置 LockoutEnabled = false - AspNetCore.Identity - How to set LockoutEnabled = false for a new user AspNetCore.Identity中的Google身份验证 - Google authentication in AspNetCore.Identity 将 AddPooledDbContextFactory 与 AspNetCore.Identity 一起使用 - Using AddPooledDbContextFactory with AspNetCore.Identity 覆盖AspNetCore.Identity表中的默认索引 - Override default indexes in AspNetCore.Identity tables AspNetCore.Identity LockoutOptions.AllowedForNewUsers属性 - AspNetCore.Identity LockoutOptions.AllowedForNewUsers Property 身份服务器 4 + AspNetCore.Identity:X 尝试后如何锁定? - identity server 4 + AspNetCore.Identity: how to lockout after X tries? AspNetCore.Identity UserManager CreateAsync无法保存关系 - AspNetCore.Identity UserManager CreateAsync cannot save relationships AspNetCore.Identity重置密码-无法跟踪“ X”的实例 - AspNetCore.Identity Reset Password - Instance of 'X' cannot be tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM