dotnet / dotnet/aspnetcore

UserManager.IsInRoleAsync(user, "rolename") still shows user in role after deleting role.

Open
#52,939 2 comments 1 reaction 0 assignees View on GitHub
area-identity investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When I tried to remove a role during a test I noticed my tests failed because `IsInRole` still shows the user in the role.
here are my test conditions
- Win 11
- .NET 8.0
- xUnit project
- MySql 8.0.27

My configs
```
services.AddDbContextPool(opt =>
{
var connection = configuration.GetConnectionString("UserDb");
opt.UseMySql(connection, ServerVersion.AutoDetect(connection),
mySql_opt => mySql_opt.EnableRetryOnFailure(3).CommandTimeout(120));
opt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});

services.AddIdentityCore(setup =>
{
setup.User.RequireUniqueEmail = false;
setup.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_@+";

setup.Password.RequireNonAlphanumeric = false;
setup.Password.RequireUppercase = false;
setup.Password.RequireLowercase = false;
setup.Password.RequireDigit = false;

//setup.Stores.ProtectPersonalData = true;

setup.Lockout.AllowedForNewUsers = true;
setup.Lockout.MaxFailedAccessAttempts = 5;
setup.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
})
.AddRoles>()
.AddRoleManager>>()
.AddEntityFrameworkStores();
```
Test Method
```
public async Task RemoveRoleSuccess(string role)
{
var email = "add-role@test.local";
var dashboard = "x";
var password = "123456";

// login admin
await LoginAdmin();

// add user
var user = await AddUser(nameof(RemoveRoleSuccess), email, true, password);
var roles = await _userManager.GetRolesAsync(user);
if (!roles.Contains(role))
await _userManager.AddToRoleAsync(user, role);
var isInRole = await _userManager.IsInRoleAsync(user, role);
Assert.True(isInRole);

var model = new RemoveRoleViewModel { Email = email, Dashboard = dashboard, Role = role };
var response = await _client.PostAsJsonAsync($"api/v1/admin/RemoveRole", model);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var body = await response.Content.ReadAsStringAsync();
Assert.NotNull(body);
Assert.IsType(body);
Assert.Contains("Role removed from user successfully", body);

roles = await _userManager.GetRolesAsync(user);
Assert.DoesNotContain(role, roles); // passed

isInRole = await _userManager.IsInRoleAsync(user, role);
Assert.False(isInRole); // failed
}
```

### Expected Behavior

I expect to see the same result from both methods `GetRoleAsync` and `IsInRoleAsync` when user is not in role.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.100

### Anything else?

These are packages I used in my project

Microsoft.AspNetCore.Identity.EntityFrameworkCore Version="8.0.0"
Microsoft.EntityFrameworkCore Version="8.0.0"
Pomelo.EntityFrameworkCore.MySql Version="8.0.0-beta.2"

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.