使用IdentityServer4与自定义配置DBContext

编程入门 行业动态 更新时间:2024-10-15 16:16:53
本文介绍了使用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

本文发布于:2023-11-15 10:57:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   DBContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!