C# 覆盖FindByEmail以按租户查找
我有一个多租户应用程序,租户共享同一个数据库,这意味着用户存储是共享的 因此,我创建了自己的C# 覆盖FindByEmail以按租户查找,c#,asp.net-core,asp.net-identity,identity,C#,Asp.net Core,Asp.net Identity,Identity,我有一个多租户应用程序,租户共享同一个数据库,这意味着用户存储是共享的 因此,我创建了自己的UserStore,如下所示: public class ApplicationUserStore : UserStore<ApplicationUser> { public int TenantId { get; set; } public ApplicationUserStore(IdentityDbContext dbContext) : base(dbCont
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);
}
}
public class ApplicationUserStore:UserStore看起来像是从2.2版中剥离出来的GetUserAggregateAsync
对于租户筛选器,您可以
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions
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);
}
}