请指导我如何自定义 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.sql
和 PersistedGrantsDb.sql 自行设置数据库
脚本位于 here (删除迁移命令并根据需要进行调整)。
下一步是在 ConfigureServices
中替换当前对 AddInMemoryClients
、AddInMemoryIdentityResources
和 AddInMemoryApiResources
的调用Startup.cs 中的方法。您将用对 IServiceCollection.AddIdentityServer
扩展方法的 AddConfigurationStore
和 AddOperationalStore
扩展的新调用替换这些调用。像这样:
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/