在OnModel中的ApplicationUser上的Queryfilter在ApplicationDbContext中创建在创建StackOverflowException时

时间:2019-08-17 14:36:18

标签: c# asp.net-core entity-framework-core asp.net-identity asp.net-core-2.1

我需要创建一个全局查询过滤器,该过滤器仅过滤那些属于某个租户的用户。

但是,在将查询过滤器添加到OnModelCreating时,出现了stackoverflow。

我使用IHttpContextAccessor从当前登录的用户获取TenantId。这在其他实体上也可以正常工作,但是ApplicationUser会创建错误。这可能是循环代码的问题吗?

我的ApplicationDbContext如下(为清楚起见而缩写)

public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>
{        
    private readonly IHttpContextAccessor _contextAccessor;

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor contextAccessor)
        : base(options)
    {
        _contextAccessor = contextAccessor;
    }

    public virtual Guid? CurrentTenantId
    {
        get
        {
            return Users.FirstOrDefault(u => u.UserName == _contextAccessor.HttpContext.User.Identity.Name)?.TenantId;
        }
    }

    public virtual string CurrentUserName
    {
        get
        {
            return Users.FirstOrDefault(u => u.UserName == _contextAccessor.HttpContext.User.Identity.Name)?.UserName;
        }
    }

    public DbSet<ApplicationUser> ApplicationUser { get; set; }
    public DbSet<Tenant> Tenant { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Tenant>().HasQueryFilter(e => e.TenantId == CurrentTenantId);
        builder.Entity<ApplicationUser>().HasQueryFilter(e => e.TenantId == CurrentTenantId);            
    }       
}

}

我在启动时将services.AddHttpContextAccessor()添加到ConfifureServices部分。

关于如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:1)

有很多方法可以解决此问题,但是我只想给你我喜欢的方法。

首先,您必须创建第二个 DbContext类,该类可以访问您的Users数据库集,并且 not 应用于它的查询过滤器。

public class UserDbContext : DbContext 
{
    public DbSet<ApplicationUser> Users { get; set; }  
}

使用与当前DbContext类相同的方式在启动时注册,并且使用相同的连接字符串。

然后,您可以继续创建将为您提供TenantID的服务。

public class TenantProvider : ITenantProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly UserDbContext _userDbContext;

    private Guid _tenantId;

    public TenantProvider(UserDbContext userDbContext,
                          IHttpContextAccessor httpContextAccessor)
    {
        _userDbContext = userDbContext;
        _httpContextAccessor = httpContextAccessor;
    }

    private void SetTenantId()
    {
        if (_httpContextAccessor.HttpContext == null)
        {
            // Whatever you would like to return if there is no request (eg. on startup of app).
            _tenantId = new Guid();
            return;
        }

        _tenantId = _userDbContext.Users.FirstOrDefault(u => u.UserName == _httpContextAccessor.HttpContext.User.Identity.Name)?.TenantId;
        return;
    }

    public Guid GetTenantId()
    {
        SetTenantId();
        return _tenantId;
    }

当然还有界面

public interface ITenantProvider
{
    Guid GetTenantId();
}

您也在启动时注册了此服务。

services.AddScoped<ITenantProvider, TenantProvider>();

然后您修改ApplicationDbContext:

public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>
{        
    private readonly IHttpContextAccessor _contextAccessor;

    private Guid _tenantId;

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor contextAccessor, ITenantProvider _tenantProvider)
        : base(options)
    {
        _contextAccessor = contextAccessor;
        _tenantId = _tenantProvider.GetTenantId();

    }

    public DbSet<ApplicationUser> ApplicationUser { get; set; }
    public DbSet<Tenant> Tenant { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Tenant>().HasQueryFilter(e => e.TenantId == _tenantId);
        builder.Entity<ApplicationUser>().HasQueryFilter(e => e.TenantId == _tenantId);            
    }       
}

应该是这样,不再有无限循环了:)祝您好运