前言

上面已经介绍了3个例子了,并且介绍了如何去使用identity。

但是在前面的例子中,我们使用的都是在内存中操作,那么正式上线可能需要持久到数据库中。

这里值得说明的是,并不一定一定要持久化到数据库中,场景不一样,需求就不一样。

那么看下如何持久化吧。

正文

例子位置:https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/5_EntityFramework

  1. 安装对应的库

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));
});
  1. 这样配置暂时没有很大的用处,因为:

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();
}
}
}

然后启动后就有了。

下一节介绍如何持久化用户数据,然后下下节,介绍各个表。

identity4 系列————持久化配置篇[五]的更多相关文章

  1. 【Windows10 IoT开发系列】配置篇

    原文:[Windows10 IoT开发系列]配置篇 Windows10 For IoT是Windows 10家族的一个新星,其针对不同平台拥有不同的版本.而其最重要的一个版本是运行在Raspberry ...

  2. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  3. [C入门 - 游戏编程系列] 贪吃蛇篇(五) - 蛇实现

    因为已经写了食物的实现,所以我不知道到底是该先写世界的实现还是蛇的实现.因为世界就是一个窗口,可以立刻在世界中看到食物的样子,对于大多数人来说,如果写完代码立刻就能看到效果,那就再好不过了.可是,我最 ...

  4. 【HANA系列】【第五篇】SAP HANA XS的JavaScript API详解

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第五篇]SAP HANA XS ...

  5. SpringBoot系列教程web篇之404、500异常页面配置

    接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,我们可以如何处理 原文友链: SpringBoot系列教程web篇之404 ...

  6. 【ABAP系列】【第五篇】SAP ABAP7.50 之用户接口

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列][第五篇]SAP ABAP7.5 ...

  7. identity4 系列————启航篇[二]

    前言 开始identity的介绍了. 正文 前文介绍了一些概念,如果概念不清的话,可以去前文查看. https://www.cnblogs.com/aoximin/p/13475444.html 对一 ...

  8. iView 实战系列教程(21课时)_1.iView 实战教程之配置篇

    1.iView 实战教程之配置篇 点击添加插件,. 选中后安装 全部导入还是按需导入. 2.是否需要自定义主题变量 3.多语言的设置. 这里我们全部选择为默认 然后点击继续. 启动项目 入口文件导入了 ...

  9. Linux服务器部署系列之八—Sendmail篇

    Sendmail是目前Linux系统下面用得最广的邮件系统之一,虽然它存在一些不足,不过,目前还是有不少公司在使用它.对它的学习,也能让我们更深的了解邮件系统的运作.下面我们就来看看sendmail邮 ...

随机推荐

  1. 迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅

    通过在互联网上收集及微软官方网站等途径获取相关资料进行整理汇总出Microsoft SQL Server各个版本(SQL Server 2008 R2.SQL Server 2012.SQL Serv ...

  2. php原生PHPExcel插件导表(附表格合并,加粗居中及加边框换行操作)

    PHPExcel是用来操作Office Excel文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言.可以使用它来读取.写入不同格式的电子表格,如 Excel(BIFF) .xls, E ...

  3. 论文阅读 Real-Time Streaming Graph Embedding Through Local Actions 11

    9 Real-Time Streaming Graph Embedding Through Local Actions 11 link:https://scholar.google.com.sg/sc ...

  4. cool-admin vite-vue3 打包部署 nginx代理设置

    location /api {rewrite ^/api/(.*)$ /$1 break;proxy_pass http://xxx.com;}location /socket.io {rewrite ...

  5. 合宙AIR105(四): SPI, MAX7219 8x8LED驱动

    目录 合宙AIR105(一): Keil MDK开发环境, DAP-Link 烧录和调试 合宙AIR105(二): 时钟设置和延迟函数 合宙AIR105(三): 定时器, 定时器中断和PWM输出 合宙 ...

  6. SAP 实例 13 Random Grouping with LOOP

    REPORT demo_loop_group_by_random. CLASS demo DEFINITION. PUBLIC SECTION. CLASS-METHODS: main, class_ ...

  7. zabbix监控apache80端口

    1.修改zabbix_agentd.conf 修改# EnableRemoteCommands=0 -->去掉注释修改为1--> EnableRemoteCommands=1 ###允许客 ...

  8. bat-注册表修改win11右键风格

    展开:reg add "HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32&q ...

  9. 一文详解|Go 分布式链路追踪实现原理

    在分布式.微服务架构下,应用一个请求往往贯穿多个分布式服务,这给应用的故障排查.性能优化带来新的挑战.分布式链路追踪作为解决分布式应用可观测问题的重要技术,愈发成为分布式应用不可缺少的基础设施.本文将 ...

  10. NC14301 K-th Number

    NC14301 K-th Number 题目 题目描述 Alice are given an array A[1..N] with N numbers. Now Alice want to build ...