首页 > 其他分享 >identity4 系列————持久化配置篇[五]

identity4 系列————持久化配置篇[五]

时间:2022-08-28 10:57:36浏览次数:108  
标签:系列 EntityFramework var add context dotnet 持久 IdentityServer4 identity4

前言

上面已经介绍了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();
		}
	}
}

然后启动后就有了。

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

标签:系列,EntityFramework,var,add,context,dotnet,持久,IdentityServer4,identity4
From: https://www.cnblogs.com/aoximin/p/16632364.html

相关文章

  • Asp.Net Core 项目部署Centos中,httpClient 请求Https报证书错误的系列问题
    参考自https://www.cnblogs.com/leoxjy/p/10201046.html#5095270Centos报这个问题,Asp.NetCore3.1HttpClient请求Https报错的SSL证书异常的问题,请使用以下方法解决......
  • RabbitMQ 入门系列:6、保障消息:不丢失:发送方、Rabbit存储端、接收方。
    系列目录RabbitMQ入门系列:1、MQ的应用场景的选择与RabbitMQ安装。RabbitMQ入门系列:2、基础含义:链接、通道、队列、交换机。RabbitMQ入门系列:3、基础含义:持久化、......
  • 【Prometheus+Grafana系列】监控MySQL服务
    前言前面的一篇文章已经介绍了docker-compose搭建Prometheus+Grafana服务。当时实现了监控服务器指标数据,是通过node_exporter。Prometheus还可用来监控很多服务,......
  • 丑数系列
    264.丑数II问题描述给你一个整数n,请你找出并返回第n个丑数。丑数就是只包含质因数 2、3和/或 5 的正整数。示例1:输入:n=10输出:12解释:[1,2,3,4,5......
  • identity4 系列————纯js客户端案例篇[四]
    前言前面已经解释了两个案例了,通信原理其实已经很清楚了,那么纯js客户端是怎么处理的呢?正文直接贴例子哈。https://github.com/IdentityServer/IdentityServer4/tree/ma......
  • 学习ASP.NET Core Blazor编程系列一——综述
    一、NET6概述   .NET6是微软统一.NetCore与.NetFramework两大框架的第二个版本,微软在.NET5中开始进行这两大框架的统一之路。   .NET6将作为长期支持......
  • 【MySQL】MySQL8持久化系统变量
    set命令可以用于将某些全局系统变量持久化到数据目录中的mysqld-auto.cnf文件中,以影响后续启动的服务器操作。resetpersist从mysqld-auto.cnf中删除持久设置。在运行时持......
  • 关于stm32f10xRB系列的PB5和PB12外设冲突问题
      上周在公司做了一个项目,调试一个mcu,本以为很简单的调试一下裸机驱动,但是调试过程中遇到了一些问题让我觉得比较有意思,记录一下。1、关于stm32的SMBUS功能的介绍  ......
  • 8、开发工具软件 - 软件技术系列文章
        在实际的软件开发和项目管理过程中,都需要很多的工具软件,使用这些软件,能够提高软件人员的工作效率,笔者在总结软件技术的时候,就收集整理了一些软件工具,以便需要的......
  • 7、软件复用 - 软件技术系列文章
        软件复用主要是为了解决在实际的软件开发过程中重复的工作,在其它软件功能相同或相近的时候,直接将代码放过去然后进行下修改即可。这个也是参考的软件定制,比如购......