Can anybody explain to me why when updating my role when I debug through the code it has the correct Guid
for the user and group?
public async Task<IActionResult> Update(RoleModification model)
{
IdentityResult result;
if (ModelState.IsValid)
{
foreach (string userId in model.AddIds ?? new string[] { })
{
ApplicationUser user = await userManager.FindByIdAsync(userId);
if (user != null)
{
result = await userManager.AddToRoleAsync(user,
model.RoleName);
if (!result.Succeeded)
{
AddErrorsFromResult(result);
}
}
}
foreach (string userId in model.DeleteIds ?? new string[] { })
{
ApplicationUser user = await userManager.FindByIdAsync(userId);
if (user != null)
{
result = await userManager.RemoveFromRoleAsync(user,
model.RoleName);
if (!result.Succeeded)
{
AddErrorsFromResult(result);
}
}
}
}
if (ModelState.IsValid)
{
return RedirectToAction(nameof(Index));
}
else
{
return await Update(model.RoleId);
}
}
This the [dbo].[AspNetRoles]
roles table as you can see ClubSuperAdmin
does exist.
ID | NAME | NormalizedName |
---|---|---|
2924519a-7024-417f-a8e1-335dca7dba63 | ClubMod | CLUBMOD |
3f6e4213-d513-47ae-8e02-dcdeaa4e22c9 | ClubUser | CLUBUSER |
40ff2868-3264-4195-82c4-68bddcc5b036 | ClubSuperAdmin | SUPERADMIN |
4b1b831c-61ac-46b9-88f5-089a11f1e46e | Admin | ADMIN |
User Table:
ID | Firstname | Last Name |
---|---|---|
f2ab370a-329a-4fde-989f-f3526334de1a | David | User |
So why is it saying that the InvalidOperationException: Role CLUBSUPERADMIN does not exist
, surley it knows the upercase of ClubSuperAdmin
is the name?
AspNetCore.Identity.UserManager.AddToRoleAsync(TUser user, string role) Warehouse.Web.Controllers.RoleController.Update(RoleModification model) in RoleController.cs
{ foreach (string userId in model.AddIds ?? new string[] { }) { ApplicationUser user = await userManager.FindByIdAsync(userId); if (user != null) { result = await userManager.AddToRoleAsync(user, model.RoleName); if (!result.Succeeded) { AddErrorsFromResult(result); } }
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor ?.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments) System.Threading.Tasks.ValueTask.get_Result()
The error is telling you that the role CLUBSUPERADMIN
does not exist and, looking at the table, that item is not there in the NormalizedName
column. I am going to assume that the original name of that role was SuperAdmin
and someone changed the table manually but didn't also update the NormalizedName
value too. To fix this, fix that value, e.g.:
UPDATE [dbo].[AspNetRoles]
SET NormalizedName = 'CLUBSUPERADMIN'
WHERE NAME = 'ClubSuperAdmin'