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
如何在多租户体系结构中使用ASP.NET Core Identity framework服务将用户添加到角色_.net_Asp.net Core_Identityserver4_Asp.net Core Identity - Fatal编程技术网

如何在多租户体系结构中使用ASP.NET Core Identity framework服务将用户添加到角色

如何在多租户体系结构中使用ASP.NET Core Identity framework服务将用户添加到角色,.net,asp.net-core,identityserver4,asp.net-core-identity,.net,Asp.net Core,Identityserver4,Asp.net Core Identity,我们正在为我们的项目实施多租户体系结构 我已经使用userManager.CreateAsync(User,register.Password)创建了用户 然后使用wait rolemager.CreateAsync(新的应用程序角色(“Admin”,ParentCompanyId)) 然后我尝试使用userManager.AddToRoleAsync(User,roleAdmin)向创建的用户添加“Admin”角色 由于我们的应用程序适用于多租户,并且在AspNetRoles表中具有租户智能管

我们正在为我们的项目实施多租户体系结构

  • 我已经使用
    userManager.CreateAsync(User,register.Password)创建了用户
  • 然后使用
    wait rolemager.CreateAsync(新的应用程序角色(“Admin”,ParentCompanyId))
  • 然后我尝试使用
    userManager.AddToRoleAsync(User,roleAdmin)向创建的用户添加“Admin”角色
  • 由于我们的应用程序适用于多租户,并且在
    AspNetRoles
    表中具有租户智能管理角色,因此
    AddToRoleAsync()
    无法获得正确的管理角色,或者在
    AspNetRoles
    表中具有模糊的“Admin”角色,因此引发以下异常。所以,任何人都可以指导我,请如何添加正确的角色,以特定的用户。由于identity framework没有任何服务可以使用
    RoleID
    将角色分配给用户,所以它会更简单。任何人都可以给出这种方法的解决方案,或者建议将用户添加到角色的正确方法

    如何在多租户体系结构中使用asp.net core Identity Framework服务将用户添加到角色。我无法使用
    userManager.AddToRoleAsync()
    ,将用户添加到管理员角色租户

    执行
    userManager.AddToRoleAsync(用户,角色)
    方法时收到以下异常

    “System.InvalidOperationException:枚举器无法移动NextAsync。 在 Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable,CancellationToken CancellationToken)位于 Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable,CancellationToken CancellationToken)位于 Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.IsInRoleAsync(TUser 用户,字符串normalizedRoleName,CancellationToken CancellationToken) 位于Microsoft.AspNetCore.Identity.UserManager`1.AddToRoleAsync(TUser 用户,字符串(角色)位于 Salon.Controller.AccountController.SignUp(RegisterViewModel注册表) 在C:\Users\Controllers\AccountController.cs中:第78行“


    方法是覆盖默认角色存储库,并在自定义角色存储库中按租户筛选角色

    对于此解决方案,我假设您使用SaasKit来解析租户,并且您的自定义标识Role具有租户属性

    如果你看一看,他们只是使用提供的RoleStore

    因此,如果查看RoleStore的属性,可以看到所有查询都是通过Roles属性完成的。通过在那里添加租户过滤器,假设租户已解析,则始终可以获得相关角色

    因此,自定义角色存储:

    public class TenantRoleStore : RoleStore<ApplicationRole<string>>
    {
        private readonly TenantContext<Tenant> _tenantContext;
    
        public TenantRoleStore(
            CustomDbContext context,
            TenantContext<Tenant> tenantContext,
            IdentityErrorDescriber describer = null)
            : base(context, describer)
        {
            _tenantContext = tenantContext;
        }
    
        public override IQueryable<ApplicationRole<string>> Roles => base.Roles.Where(r => r.Tenant.Id == _tenantContext.Tenant.Id);
    }
    
    公共类租户存储:角色存储
    {
    私有只读租户上下文\u租户上下文;
    公共租客(
    CustomDbContext上下文,
    租户上下文租户上下文,
    IdentityErrorDescriber描述符=空)
    :base(上下文、描述符)
    {
    _租户上下文=租户上下文;
    }
    public override IQueryable Roles=>base.Roles.Where(r=>r.Tenant.Id==\u tenantContext.Tenant.Id);
    }
    
    在启动中,只需将其添加到配置标识的位置:

    services.AddIdentity()
        .AddRoleStore<TenantRoleStore>(); // <- add this
    
    services.AddIdentity()
    
    .AddRoleStore();//你试过什么?作为代码Imean@Mayk:我已详细更新了我的问题,因此每个角色都连接到租户,对吗?因此,您可以有多个管理员角色,这些角色来自不同的租户?你是如何解决房客问题的?