这允许 KVP 和多个值。
// with Razor, you did not specific if it was core 2, 3.1 or Razor
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
services.AddAuthorization(options =>
{
options.AddPolicy("Vendors", policy =>
policy.RequireClaim("Type.Tykt.org", "Dealer", "Driver", "WholeSaler", "Asset", "Repair"));
});
}
选项 2:
还有一个claims
集合,用户成功登录后可以添加。
var user = new User {
Email = "xyz@tykt.org",
Name = "xyz"
}
user.Claims.Add(new IdentityUserClaim<string>
{
ClaimType="your-type", // your key
ClaimValue="your-value" // your value
});
await userManager.CreateAsync(user);
更新参考 MSDN:
如果我理解了这个问题,那么您对如何存储检索确实是您的选择,那么您的问题就是索赔的价值。
通常,映射和验证以类似于 PermissionHandler : IAuthorizationHandler
或通用方法的方式发生MinimumAgeHandler : AuthorizationHandler<MinimumAgeRequirement>
。其中,加载值,并处理特定权限的需求验证,例如最小年龄,但实际声明(您在说明什么/min age policy
与值通常在数据库中,如 DOB= 1/1/1990
)随Principal
对象一起移动。现在,您选择在哪里检索索赔的价值取决于您
在下面的函数中,他从 Context 中获取 Key 的值,然后进行验证
public class MinimumAgeHandler : AuthorizationHandler<MinimumAgeRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
MinimumAgeRequirement requirement)
{
if (!context.User.HasClaim(c => c.Type == ClaimTypes.DateOfBirth &&
c.Issuer == "http://contoso.com"))
{
return Task.CompletedTask;
}
var dateOfBirth = Convert.ToDateTime(
// He gets the value on the server-side from any store or 3rd party relayer
context.User.FindFirst(c => c.Type == ClaimTypes.DateOfBirth &&
c.Issuer == "http://contoso.com").Value);
int calculatedAge = DateTime.Today.Year - dateOfBirth.Year;
if (dateOfBirth > DateTime.Today.AddYears(-calculatedAge))
{
calculatedAge--;
}
if (calculatedAge >= requirement.MinimumAge)
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}