覆盖 FindByEmail 以按租户查找



我有一个多租户应用程序,租户共享同一个数据库,这意味着用户存储是共享的。

因此,我创建了自己的UserStore,如下所示:

public class ApplicationUserStore : UserStore<ApplicationUser>
{
    public int TenantId { get; set; }
    public ApplicationUserStore(IdentityDbContext dbContext)
    : base(dbContext)
    {
    }
    public override Task<IdentityResult> CreateAsync(ApplicationUser user, CancellationToken cancellationToken = default)
    {
        user.TenantId = TenantId;
        return base.CreateAsync(user, cancellationToken);
    }
    public override Task<ApplicationUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken = default)
    {
        return base.FindByEmailAsync(normalizedEmail, cancellationToken);
    }
}

遇到的问题是,我不确定如何添加到FindByEmailAsync调用中,以使用相应的电子邮件和租户来获取用户。

我应该如何解决这个问题?我正在使用 .NET Core 2.2 并查看本指南:https://www.scottbrady91.com/ASPNET-Identity/Quick-and-Easy-ASPNET-Identity-Multitenancy?fbclid=IwAR3F2NmmCoSHfxvIPwpQ0l-gTthFfVICaTdU2etcHyN--UEm-Nd6OP0LnLE 看起来GetUserAggregateAsync已被从 2.2 中删除

对于租户筛选器,可以尝试全局查询筛选器,例如

public class ApplicationDbContext : IdentityDbContext<SystemUser,IdentityRole<int>,int>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public int TenantId { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
        builder.Entity<SystemUser>().HasQueryFilter(b => EF.Property<int>(b, "TenantId") == TenantId);
    }
}

否则,您可能需要通过引用 GetUserAggregateAsync 来实现自己的GetUserAggregateAsync

相关内容

  • 没有找到相关文章

最新更新