使用IdentityServer4与自定义配置DBContext [英] using IdentityServer4 with custom Configration DBContext

查看:1652
本文介绍了使用IdentityServer4与自定义配置DBContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Oracle中使用IDS4,我创建了一个定制的 IConfigurationDbContext

I created a customized IConfigurationDbContext in order to using IDS4 with Oracle.

  public class IdentityConfigurationDbContext :  DbContext, IConfigurationDbContext {
        private readonly ConfigurationStoreOptions storeOptions;

        public IdentityConfigurationDbContext(DbContextOptions<IdentityServerDbContext> options)
         : base(options) {
    }

    public IdentityConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions)
        : base(options) {
        this.storeOptions = storeOptions ?? throw new ArgumentNullException(nameof(storeOptions));
    }

    public DbSet<Client> Clients { get; set; }
    public DbSet<IdentityResource> IdentityResources { get; set; }
    public DbSet<ApiResource> ApiResources { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.ConfigureClientContext(storeOptions);
        modelBuilder.ConfigureResourcesContext(storeOptions);

        base.OnModelCreating(modelBuilder);
    }
  }

在ConfigureService中:

in ConfigureService:

 services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddAspNetIdentity<ApplicationUser>();

我也有我的自定义 IClientStore 如下所示添加到容器中:

I also have my custom IClientStore which is added to the container like this:

services.AddScoped<IClientStore, ClientStore>();

当我运行 IdentityConfigurationDbContext 迁移时,我得到这个错误:

when I run IdentityConfigurationDbContext migration, I get this error:

System.InvalidOperationException: No database provider has been configured for this DbContext.

我试过这样做:

services.AddDbContext<IdentityConfigurationDbContext>(builder => builder.UseOracle(connectionString, options => {
                options.MigrationsAssembly(migrationsAssembly);
                options.MigrationsHistoryTable("EF_MIGRATION_HISTORY");
            }));

这是使用IDS4定制dbcontext的正确方法吗?我如何解决这个问题,并完成我的迁移工作?

Is this the right way to use a custom dbcontext with IDS4? and How do I fix this issue, and complete my migration work?

推荐答案

我尝试了一种不同的方法。而不是实现 IConfigurationDbContext 我已经从 IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext继承

I've tried a different approach. Instead of implementing IConfigurationDbContext I have inherited from IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext

public class CustomConfigurationDbContext : ConfigurationDbContext
{
    public CustomConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options,
        ConfigurationStoreOptions storeOptions)
        : base(options, storeOptions)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            //...

            base.OnConfiguring(optionsBuilder);
        }
    }
}

在startup.cs

And in the startup.cs

services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddConfigurationStore(
                    builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly)))
                .AddOperationalStore(
                    builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly)))
                .AddAspNetIdentity<ApplicationUser>();

它的作用就像一个魅力。
免责声明:这不是我的想法。我只是不记得那个的来源。

It works like a charm. Disclaimer: this is not my idea. I just cannot remember the source of that.

这篇关于使用IdentityServer4与自定义配置DBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆