如何获取令牌声明值并注入dbcontext服务

Lui*_*aus 5 c# asp.net entity-framework asp.net-core ef-core-2.0

我有一个DbContext,我需要一个CompanyId来进行全局查询,比如多租户.

    public void SetGlobalQuery<T>(ModelBuilder builder) where T : BaseEntity
    {
        builder.Entity<T>().HasQueryFilter(e => e.CompanyId == _companyId);
    }
Run Code Online (Sandbox Code Playgroud)

CompanyId被添加到令牌声明中,我如何获得声明值并注入DbContext?

    services.AddScoped(provider => {
        var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
        var options = optionsBuilder.UseSqlServer(Configuration.GetConnectionString("MyDbDatabase")).Options;
        var companyId = 1;
        return new DocContext(options, companyId);
    });
Run Code Online (Sandbox Code Playgroud)