c# - 使用数据库而不是内存存储身份服务器 4

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

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

需要覆盖的类列表以及如何配置它们

最佳答案

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

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


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

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

下一步是在 ConfigureServices 中替换当前对 AddInMemoryClientsAddInMemoryIdentityResourcesAddInMemoryApiResources 的调用Startup.cs 中的方法。您将用对 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);
        });
}

关于c# - 使用数据库而不是内存存储身份服务器 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113336/

相关文章:

C#加密与解密

c# - 在代码隐藏中绑定(bind)动态创建的控件

asp.net - asp页面和paypal按钮

reactjs - Visual Studio 2019 测试资源管理器中的 Jest 单元测试

asp.net-mvc - JWT token 通常对于使用跨源有效吗?

C# 参数化方法数组或参数化方法列表

c# - WPF DataGridColumn 条件控件

javascript - 为什么我无法使用 JSON 数据?

javascript - 无法在 ASP.NET Core 中使用 D3 加载 .geojson 文件

c# - 如何在 oData .Net core 3.1 中启用 $levels