確保您的數據庫中有必要的數據。您必須確定該角色位於「AspNetRoles」表中,並且該用戶已鏈接到「AspNetUserRoles」表中。爲確保您有數據,您可以使用以下種子:
public static class RoleSeeder
{
public static void Seed()
{
CreateRole("Admin", "Administratie", "Administratie");
...
}
private static void CreateRole(string name, string description, string group)
{
using (var db = new DbContext())
{
if (!db.Roles.Any(r => r.Name == name))
{
var role = new Role { Name = name, Description = description, Group = group };
var store = new RoleStore<Role>(db);
var manager = new RoleManager<Role>(store);
manager.Create(role);
}
else
{
var store = new RoleStore<Role>(db);
var manager = new RoleManager<Role>(store);
var dbRole = manager.FindByName(name);
dbRole.Description = description;
dbRole.Group = group;
manager.Update(dbRole);
}
}
}
}
沒有理由編寫您自己的角色管理器。
您需要設置角色管理器,請參閱此答案:https://stackoverflow.com/a/39139606/5519026 – Ziyad