如何添加自定义声明类型

时间:2019-12-02 11:24:23

标签: asp.net-core asp.net-core-webapi

var ManagerAccess = context.Principal.FindFirst(ClaimTypes.System).Value;

在这里我要这样执行: (claimstypes.ManagerAccess).value;

这可能吗?

1 个答案:

答案 0 :(得分:0)

要添加自定义声明类型,您可以使用IClaimsTransformation

public class CustomClaimsTransformer : IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("ManagerAccess", "MyValue"));
        return Task.FromResult(principal);
    }
}

注册startup.cs

services.AddScoped<IClaimsTransformation, CustomClaimsTransformer>();

检查您的索赔:

var ManagerAccess = context.Principal.FindFirst("ManagerAccess").Value;

有关更多解决方案,请参阅Implementing custom claim with extended MVC Core Identity user