单个策略的要求
只要满足“Edit Role”声明,策略便成功:
options.AddPolicy("EditRolePolicy", policy => policy.RequireClaim("Edit Role"));
必须拥有“Edit Role”声明,且值为 true:
options.AddPolicy("EditRolePolicy", policy => policy.RequireClaim("Edit Role", "true"));
指定了多个声明值,用户在拥有“Edit Role”声明时,值为 true 或 yes 皆可:
options.AddPolicy("EditRolePolicy", policy => policy.RequireClaim("Edit Role", "true", "yes"));
多个授权要求,声明和角色组合使用
用户拥有 Admin 角色和“Edit Role”声明,然后声明值为“true”或“yes”皆可:
options.AddPolicy("EditRolePolicy", policy => policy
.RequireClaim("Edit Role", "true", "yes")
.RequireRole("Admin"));
必须拥有“Admin”角色及声明类型为“Edit Role”声明值为 true,或拥有“Super Admin”角色:
options.AddPolicy("EditRolePolicy",
policy => policy.RequireAssertion(context =>
context.User.IsInRole("Admin") && context.User.HasClaim(claim => claim.Type == "Edit Role" && claim.Value == "true") ||
context.User.IsInRole("Super Admin")));