覆盖 FindByEmail 以按租户查找
Overriding FindByEmail to find by tenant too
我有一个多租户应用程序,其中租户共享同一个数据库,因此意味着用户存储是共享的。
因此,我创建了自己的 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
中删除
对于租户过滤器,您可以尝试 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
我有一个多租户应用程序,其中租户共享同一个数据库,因此意味着用户存储是共享的。
因此,我创建了自己的 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
对于租户过滤器,您可以尝试 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