即使存在于数据库中也找不到角色
Role not being found even though exists in the database
任何人都可以向我解释为什么在我通过代码进行调试时更新我的角色时它具有正确的用户和组 Guid
吗?
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);
}
}
如您所见,[dbo].[AspNetRoles]
角色 table ClubSuperAdmin
确实存在。
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
用户Table:
ID
Firstname
Last Name
f2ab370a-329a-4fde-989f-f3526334de1a
David
User
那为什么说InvalidOperationException: Role CLUBSUPERADMIN does not exist
,surley它知道ClubSuperAdmin
的大写是名字呢?
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()
错误告诉您角色 CLUBSUPERADMIN
不存在,并且查看 table,该项目不在 NormalizedName
列中。我将假设该角色的原始名称是 SuperAdmin
并且有人手动更改了 table 但也没有更新 NormalizedName
值。要解决此问题,请修复该值,例如:
UPDATE [dbo].[AspNetRoles]
SET NormalizedName = 'CLUBSUPERADMIN'
WHERE NAME = 'ClubSuperAdmin'
任何人都可以向我解释为什么在我通过代码进行调试时更新我的角色时它具有正确的用户和组 Guid
吗?
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);
}
}
如您所见,[dbo].[AspNetRoles]
角色 table ClubSuperAdmin
确实存在。
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 |
用户Table:
ID | Firstname | Last Name |
---|---|---|
f2ab370a-329a-4fde-989f-f3526334de1a | David | User |
那为什么说InvalidOperationException: Role CLUBSUPERADMIN does not exist
,surley它知道ClubSuperAdmin
的大写是名字呢?
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()
错误告诉您角色 CLUBSUPERADMIN
不存在,并且查看 table,该项目不在 NormalizedName
列中。我将假设该角色的原始名称是 SuperAdmin
并且有人手动更改了 table 但也没有更新 NormalizedName
值。要解决此问题,请修复该值,例如:
UPDATE [dbo].[AspNetRoles]
SET NormalizedName = 'CLUBSUPERADMIN'
WHERE NAME = 'ClubSuperAdmin'