覆盖FindByEmail也可以通过租户查找

时间:2019-07-13 21:12:47

标签: c# asp.net-core asp.net-identity identity

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

因此,我像这样创建了自己的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中剥离出来

1 个答案:

答案 0 :(得分:0)

对于租户过滤器,您可以像

那样尝试Global Query Filters
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