我在使用角色/声明时遇到问题。
我已经创建了角色并给出了角色声明。然后将这些角色分配给用户,从我在线阅读的内容来看,这意味着用户应该继承角色声明,但他们没有。该策略不起作用,经过进一步检查,我在通过 JSON 输出用户声明时看不到声明。
正如我所看到的,所有数据都保存在数据库中。
角色/索赔播种机
public static void SeedRolesAndClaims(RoleManager<IdentityRole> roleManager)
{
// Create Roles
IdentityRole adminRole = new IdentityRole("Admin");
roleManager.CreateAsync(adminRole).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "edit.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "delete.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "create.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "view.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "create.comment")).Wait();
IdentityRole userRole = new IdentityRole("User");
roleManager.CreateAsync(userRole).Wait();
roleManager.AddClaimAsync(userRole, new Claim(ClaimTypes.AuthorizationDecision, "create.comment")).Wait();
}
Run Code Online (Sandbox Code Playgroud)
用户播种机
ApplicationUser user = new ApplicationUser { UserName = "john@email.com", FirstName = "Admin", LastName = "Smith", Email = "john@email.com" };
userManager.CreateAsync(user, "Password123*").Wait();
userManager.AddToRoleAsync(user, "Admin").Wait();
Run Code Online (Sandbox Code Playgroud)
我正在做的检查是
var claims = …Run Code Online (Sandbox Code Playgroud)