2

我一直在使用一些示例在 ASP.NET Core 中使用 Identity,这让我很沮丧。尽管角色模型有一个主键Id和一个字符串Name,但它是由各种内置服务传递的名称。

例如,当我打电话时,_userManager.GetRolesAsync(user)我会返回一个字符串名称列表。如果我想要身份证怎么办?没有用于此的 API,因此我必须使用角色管理器来获取所有角色并映射它们。默认情况下,角色名称不受唯一键的约束,因此这是不可靠的。

我一定是错过了什么,但是什么?我现在的用例是我想用用户的角色填充 JWT 声明。就我而言,角色名称是我的应用程序内部的,我不想将这些暴露给客户。

4

1 回答 1

3

对于_userManager.GetRolesAsync(user),它调用userRoleStore.GetRolesAsync

        public virtual async Task<IList<string>> GetRolesAsync(TUser user)
    {
        ThrowIfDisposed();
        var userRoleStore = GetUserRoleStore();
        if (user == null)
        {
            throw new ArgumentNullException(nameof(user));
        }
        return await userRoleStore.GetRolesAsync(user, CancellationToken);
    }

对于返回角色 id 而不是角色名称,您可以userRoleStore.GetRolesAsync通过实现自己的UserStore.

  • 用户存储.cs

               public class CustomUserStore : UserStore<IdentityUser>
    {
        public CustomUserStore(DbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
        {
        }
    
        public override async Task<IList<string>> GetRolesAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken))
        {
            var roleNames = await base.GetRolesAsync(user, cancellationToken);
            var roleIds = await Context.Set<IdentityRole>()
                                 .Where(r => roleNames.Contains(r.Name))
                                 .Select(r => r.Id)
                                 .ToListAsync();
    
            return roleIds;
        }
    }
    
  • 注册用户存储

        services.AddDefaultIdentity<IdentityUser>()
            .AddRoles<IdentityRole>()
            .AddUserStore<CustomUserStore>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            ;
        services.AddScoped<DbContext, ApplicationDbContext>();
    
  • 然后您将通过以下方式获取角色 ID 集合

        var user = await _userManager.FindByNameAsync(User.Identity.Name);
        var roles = await _userManager.GetRolesAsync(user);
    
于 2018-11-20T06:25:36.550 回答