Skip to content

RoleExistsAsync method not working in Identity RoleManager  #35946

Closed
@faresamr

Description

@faresamr

Describe the bug

roleManager.RoleExistsAsync("myRole") always returns null value as if the role doesn't exist even if its there.

🙌 I will be glad to contribute a fix for it and check if there is any other methods do the same thing

This happens because of role name gets normalized twice, first RoleExistsAsync normalize the parameter value then pass it to FindByNameAsync where it gets normalized for the second time which leads to a normalized value doesn't exists

public virtual async Task<bool> RoleExistsAsync(string roleName)
{
ThrowIfDisposed();
if (roleName == null)
{
throw new ArgumentNullException(nameof(roleName));
}
return await FindByNameAsync(NormalizeKey(roleName)) != null;
}

public virtual Task<TRole> FindByNameAsync(string roleName)
{
ThrowIfDisposed();
if (roleName == null)
{
throw new ArgumentNullException(nameof(roleName));
}
return Store.FindByNameAsync(NormalizeKey(roleName), CancellationToken);
}

This case was not covered by unit test because of UpperInvariantLookupNormalizer is being used as normalizer which dose not make any deference if the value gets normalized more than once, but my case I'm using hashing

To Reproduce

await roleManager.CreateAsync(new IdentityRole("myRole"));
IdentityRole role = await roleManager.RoleExistsAsync("myRole");
//role is null

Exceptions (if any)

No Exceptions.

Further technical details

  • ASP.NET Core 5.0

Metadata

Metadata

Assignees

Labels

✔️ Resolution: FixedThe bug or enhancement requested in this issue has been checked-in!DoneThis issue has been fixedarea-identityIncludes: Identity and providershelp wantedUp for grabs. We would accept a PR to help resolve this issueinvestigate

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions