【问题标题】:Using database instead of in memory store Identity server 4使用数据库而不是内存存储身份服务器 4
【发布时间】:2016-05-09 10:23:29
【问题描述】:

请指导我如何自定义 identityserver 4 以使用数据库而不是内存存储。

需要重写的类列表以及如何配置它们

【问题讨论】:

  • 你得到什么了吗?我试图通过 identityserver4 社区在 gitter 上问这个问题。但似乎没有人对此作出任何建设性的答复。我相信,由于 IdSrv 刚刚处于测试阶段,这就是为什么这些东西正在移植到 identityserver4 的过程中。对于这些事情,我们将不得不等待它的 RC。

标签: c# asp.net asp.net-core authentication identityserver4


【解决方案1】:

我知道这是一个较老的问题,但它没有答案,其他人可能会像我一样偶然发现这个问题并需要解决方案。

这是一个 walk through guide for setting up database usage via entity framework,它是 Identity Server 4 文档的一部分。如果您刚刚开始使用 Identity Server,那么可以将模板 IS4 项目设置为通过开箱即用的实体框架使用数据库存储。转到here 并查看dotnet new is4ef 模板的信息


将内存存储转换为数据库存储

如果您已经在使用内存存储 Identity Server 4 项目,那么基本上上面的演练将让您安装 IdentityServer4.EntityFramework Nuget 包。然后,您需要运行 EF Migrations 来创建数据库来存储 Identity Server 配置和操作数据,或者可以选择使用位于 ConfigurationDb.sqlPersistedGrantsDb.sql 的脚本自行设置数据库here(删除迁移命令和根据需要进行调整)。

下一步是在 Startup.csConfigureServices 方法中替换当前对 AddInMemoryClientsAddInMemoryIdentityResourcesAddInMemoryApiResources 的调用。您将在 IServiceCollection.AddIdentityServer 扩展方法上用对 AddConfigurationStoreAddOperationalStore 扩展的新调用替换这些调用。像这样:

public void ConfigureServices(IServiceCollection services)
{
    //other configuration code ...
    
    string connectionString = "your DB connectionString";
    
    services.AddIdentityServer()
        // this adds the config data DB support (clients, resources, CORS)
        .AddConfigurationStore(options =>
        {
            options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString);
        })
        // this adds the operational data DB support (codes, tokens, consents)
        .AddOperationalStore(options =>
        {
            options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString);
        });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多