【问题标题】:How to read clients and other configs from database in Duende/Identity Server?如何从 Duende/Identity Server 的数据库中读取客户端和其他配置?
【发布时间】:2023-02-19 22:09:37
【问题描述】:

我对 Duende/Identity Sever 身份验证有疑问。我一直在使用 config.cs 来存储客户端和其他配置。 今天,我将它们迁移到身份服务器数据库。现在,我想摆脱 Config.cs 并直接从数据库中读取配置,但我不知道如何访问 ConfigureDbContext 及其实体。 我想我应该像下面那样对 program.cs 进行一些更改,但无法弄清楚我应该使用什么来代替下面的那些注释行。

builder.Services.AddIdentityServer()
 .AddConfigurationStore(options =>
 {
    options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
        sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
    options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
        sql => sql.MigrationsAssembly(migrationsAssembly));
})
//.AddInMemoryIdentityResources(Config.IdentityResources)
//.AddInMemoryApiScopes(Config.ApiScopes)
//.AddInMemoryClients(Config.Clients)
.AddMyUserStore();

你能帮我解决这个问题吗?

【问题讨论】:

    标签: identityserver4 duende


    【解决方案1】:

    在 duende.identity server 6.2 中,我在没有 AddInMemery() 的情况下配置如下: 步骤1 创建新的迁移(因为它们被添加了新表和新字段)。 PersistedGrant 方案的脚本:

    dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
    

    配置方案的脚本:

    dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
    

    配置身份服务器:

     builder.Services.AddIdentityServer()
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = b => b.UseSqlite(connectionString,
                    sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = b => b.UseSqlite(connectionString,
                    sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            .AddTestUsers(TestUsers.Users);
    

    正如我们在没有 .AddInMemoryIdentityResources(Config.IdentityResources).AddInMemoryApiScopes(Config.ApiScopes).AddInMemoryClients(Config.Clients) 时看到的那样。

    将客户端配置添加到我们的数据库:

    private static void InitializeDatabase(IApplicationBuilder app)
    {
        using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
        {
            serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
    
            var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
            context.Database.Migrate();
            if (!context.Clients.Any())
            {
                foreach (var client in Config.Clients)
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
    
            if (!context.IdentityResources.Any())
            {
                foreach (var resource in Config.IdentityResources)
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
    
            if (!context.ApiScopes.Any())
            {
                foreach (var resource in Config.ApiScopes)
                {
                    context.ApiScopes.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
    }
    

    从 ConfigurePipeline 方法调用 InitializeDatabase:

    public static WebApplication ConfigurePipeline(this WebApplication app)
    { 
        app.UseSerilogRequestLogging();
        if (app.Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        InitializeDatabase(app);
        
        //...
    }
    

    您可以在官方文档中阅读更多内容。 Link to official documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多