请指导我如何自定义 identityserver 4 以使用数据库而不是内存存储。
需要覆盖的类列表以及如何配置它们
请指导我如何自定义 identityserver 4 以使用数据库而不是内存存储。
需要覆盖的类列表以及如何配置它们
我知道这是一个较老的问题,但它没有答案,其他人可能会像我一样偶然发现这个问题并需要一个解决方案。
这是通过作为 Identity Server 4 文档的一部分的实体框架设置数据库使用的演练指南。如果您刚刚开始使用 Identity Server,那么有一个模板 IS4 项目可以设置为通过开箱即用的实体框架使用数据库存储。去这里查看dotnet new is4ef
模板的信息
如果您已经在使用内存存储 Identity Server 4 项目,那么基本上上面的演练将让您安装IdentityServer4.EntityFramework
Nuget 包。然后,您将需要运行 EF Migrations 来创建数据库以存储 Identity Server 配置和操作数据,或者可以选择使用此处ConfigurationDb.sql
的PersistedGrantsDb.sql
脚本自行设置数据库(删除迁移命令并根据需要进行调整)。
下一步是替换当前对Startup.cs方法中的AddInMemoryClients
、AddInMemoryIdentityResources
和AddInMemoryApiResources
的调用。您将用对扩展方法上的和扩展的新调用替换这些调用。像这样:ConfigureServices
AddConfigurationStore
AddOperationalStore
IServiceCollection.AddIdentityServer
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);
});
}