前言

上面已经介绍了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. 探索链路追踪在.NET6工业物联网项目的应用

    ExploringIoTDistributedTracingNet6 如果觉得有用,请留言学到了. 已经会了的老哥,请留言就这? 可能遇到的问题 工业物联网项目自上而下一般分为ERP.Mes.SCAD ...

  2. opencv-python保存视频

    import cv2 class WVideoManager: def __init__(self, write_path: str, width: int, height: int, FPS: in ...

  3. 安装gitlab客户端

    1. 下载客户端软件包 https://pan.baidu.com/disk/home#/category?type=6&vmode=list 安装顺序: Git-2.13.3-64-bit. ...

  4. C#中List实体类转换为object 并把参数返回到前端

    用ConvertAll方法转换: List<Object> m= list.ConvertAll(s=> (object)s); 返回的结果:

  5. 事务@Transactional注解的属性

    事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行.事务的传播行为可以由传播属性指定.Sprin ...

  6. throw关键字和Objects非空判断_requireNonNull方法

    作用: 可以使用throw关键字在指定的方法中抛出指定的异常 使用格式: throw new xxxException("异常产生的原因") 注意: 1.throw关键字必须写在方 ...

  7. python requests 使用代理池访问https站点返回乱码

    问题表现: 检查一下正常的请求头里面accept-encoding字段是否包含br,如果包含,果断pip install urllib3[brotli],详见ssl-warnings 记录另外一个问题 ...

  8. css基础02

    熟练快捷键!方便,要多练!  css复合选择器 不会选孙子,有一个儿子和另一个儿子的孩子(也是孙子)同名了,但子选择器子选择儿子,同名的孙子不选.和后代选择器有一点不一样的. " ,&quo ...

  9. PHP切割汉字

    <?php /* @UTF-8编码的字符可能由1~3个字节组成. */ /*--------------------------方法一截取中文字符串方法--------------------- ...

  10. YII学习总结3(session)

    session操作 <?php namespace app\controllers; use yii\web\Controller; class HelloController extends ...