-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I created a role using the _roleManager.CreateAsync(adminRole), and I'm able read the role via await _roleManager.FindByNameAsync(RoleConstants.Admin), but getting exception that role does not exist when adding a user to the role by calling the await _userManager.AddToRoleAsync(adminUser, RoleConstants.Admin);
Checked the query for getting the role I realized that the method is querying against the normalized name. See query below:
SELECT "r"."Id", "r"."ConcurrencyStamp", "r"."CreatedBy", "r"."CreatedOn", "r"."Description", "r"."LastModifiedBy", "r"."LastModifiedDate", "r"."Name", "r"."NormalizedName", "r"."TenantId"
FROM "Role" AS "r"
WHERE (("r"."TenantId" IS NULL) OR ("r"."TenantId" = @__ef_filter__TenantId_0)) AND ("r"."NormalizedName" = @__normalizedRoleName_0)
LIMIT 2
Expected Behavior
The AddToRoleAsync method should query as the role manager find by name method, so that user can be added to a role without having to pass the normalized value.
Steps To Reproduce
var admin = await _userManager.FindByEmailAsync("[email protected]");
if (admin==null)
{
admin = new SkoolzUser
{
Active=true,
Email = "[email protected]",
EmailConfirmed=true,
UserName = "sysuser",
};
await _userManager.CreateAsync(admin, "password");
}
if (!await _userManager.IsInRoleAsync(admin, RoleConstants.Admin)) //Error here and below if this condition is not checked.
await _userManager.AddToRoleAsync(admin, RoleConstants.Admin);
Exceptions (if any)
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Internal.EntityFinder1.FindTracked(Object[] keyValues, IReadOnlyList1& keyProperties)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder1.FindAsync(Object[] keyValues, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.FindAsync(Object[] keyValues, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9.FindUserRoleAsync(TKey userId, TKey roleId, CancellationToken cancellationToken) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9.IsInRoleAsync(TUser user, String normalizedRoleName, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.UserManager`1.IsInRoleAsync(TUser user, String role)
at Skoolz.Infrastructure.DataSeed.AdminUser() in C:\Users\Zach\Documents\Dev\Projects\Skoolz\Skoolz.Infrastructure\DataSeed.cs:line 66
at Skoolz.Infrastructure.DataSeed.Initialize() in C:\Users\Zach\Documents\Dev\Projects\Skoolz\Skoolz.Infrastructure\DataSeed.cs:line 37
.NET Version
6.0.400
Anything else?
No response