Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET核心标识中使用角色GUID而不是名称?_C#_Asp.net Core_Asp.net Identity - Fatal编程技术网

C# 如何在ASP.NET核心标识中使用角色GUID而不是名称?

C# 如何在ASP.NET核心标识中使用角色GUID而不是名称?,c#,asp.net-core,asp.net-identity,C#,Asp.net Core,Asp.net Identity,我一直在ASP.NETCore中使用一些示例玩弄Identity,有些东西让我很沮丧。尽管角色模型有一个主键Id和一个字符串Name,但它是各种内置服务传递的名称 public class CustomUserStore : UserStore<IdentityUser> { public CustomUserStore(DbContext context, IdentityErrorDescriber describer = null) : base(

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

           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;
    }
}
例如,当我调用
\u userManager.GetRolesAsync(user)
时,我会得到一个字符串名列表。如果我要身份证怎么办?没有用于此的API,因此我必须使用角色管理器来获取所有角色并映射它们。默认情况下,角色名称不受唯一键的约束,因此这是不可靠的

           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;
    }
}

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

对于
\u 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);
    }
           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;
    }
}
  • 注册用户存储

               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怎么办?
    这是什么意思?是否要获取用户角色id?就像
    \u dbContext.Roles.Single(x=>x.Name==“Administrator”)
    ?我想我的问题很清楚,对不起。我想通过角色的
    Id
    属性(在我的例子中是一个字符串化的GUID)引用角色,而不是通过角色的
    Name
    属性引用角色。