identity4 系列————持久化配置篇[五]
前言
上面已经介绍了3个例子了,并且介绍了如何去使用identity。
但是在前面的例子中,我们使用的都是在内存中操作,那么正式上线可能需要持久到数据库中。
这里值得说明的是,并不一定一定要持久化到数据库中,场景不一样,需求就不一样。
那么看下如何持久化吧。
正文
例子位置:https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/5_EntityFramework
- 安装对应的库
IdentityServer4.EntityFramework
dotnet add package IdentityServer4.EntityFramework
安装对应的:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
官网中例子使用了sql server localdb。 那么需要安装 Microsoft.EntityFrameworkCore.SqlServer.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
identity server 的配置在这,那么我们处理的应该就是这么一部分。
改成下面这样:
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-4.0.0;trusted_connection=yes;";
var builder = services.AddIdentityServer()
.AddTestUsers(TestUsers.Users)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
- 这样配置暂时没有很大的用处,因为:
The IdentityServer4.EntityFramework.Storage package contains entity classes that map from IdentityServer’s models.
IdentityServer4.EntityFramework.Storage 存储的是实体类,相当于是code first,那么这个得做迁移了。
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
加入工具和相应的库。
注:这里就不解释太多了,后面介绍ef code first 会介绍其中的原理之类的
然后做好迁移。
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
这样那么数据库就创建好了。
然后初始化一些数据:
private 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 scope in Config.ApiScopes)
{
context.ApiScopes.Add(scope.ToEntity());
}
context.SaveChanges();
}
}
}
然后启动后就有了。
结
下一节介绍如何持久化用户数据,然后下下节,介绍各个表。
分类:
.net core(web)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
2021-08-28 redis 简单整理——持久化之RDB[十九]
2020-08-28 平时写文档的文档要求