ASP.NET Core Identity Role, Claim and User

前端 未结 2 1357
星月不相逢
星月不相逢 2021-01-11 14:14

I am an ASP.NET Core beginner. I\'m stuck in role, claim and user relationship.

I have a user Ben, user belongs to Admin role.

2条回答
  •  一个人的身影
    2021-01-11 14:38

    Why userManager.GetClaimsAsync(user) returns empty claims ?

    Because UserManager.GetClaimsAsync(user) queries the UserClaims table. Same for RoleManager.GetClaimsAsync(role) queries the RoleClaims table.

    But by design in ASP.NET Identity Core when a user is a member of a role, they automatically inherit the role's claims. You can check the ClaimsPrincipal, for example inside a controller action:

    var claims = User.Claims.ToList();
    

    You can see the code in UserClaimsPrincipalFactory.cs that creates a ClaimsPrincipal from an user.

提交回复
热议问题