分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

使用 ASP.NET Core Identity 的 IdentityServer4 授权服务器(二)

发布时间:2023-09-06 02:05责任编辑:沈小雨关键词:.NET

在 使用 ASP.NET Core Identity 的 IdentityServer4 授权服务器(1) 中,IdentityServer4 使用的是内存数据,不方便灵活,这次要把 IdentityServer4 的数据也保存到数据库中。

添加 IdentityServer4.EntityFramework

IdentityServer4 有两种类型的数据需要保存数据库中。第一是配置数据(资源和客户端)。第二个是 IdentityServer 在使用时产生的操作数据(令牌,代码和同意)。这些存储使用接口建模, Nuget包中的 IdentityServer4.EntityFramework 提供这些接口的EF实现。
添加 IdentityServer4.EntityFramework

修改 Startup.cs

Startup.csConfigureServices方法中添加以及修改以下代码:

var connectionString = Configuration.GetConnectionString("DefaultConnection");var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;services.AddIdentityServer() ???.AddDeveloperSigningCredential() ???//.AddInMemoryPersistedGrants() ???//.AddInMemoryIdentityResources(Config.GetIdentityResources()) ???//.AddInMemoryApiResources(Config.GetApiResources()) ???//.AddInMemoryClients(Config.GetClients()) ???.AddConfigurationStore(options => ???????{ ???????????options.ConfigureDbContext = builder => ???????????????builder.UseMySQL(connectionString, ???????????????sql => sql.MigrationsAssembly(migrationsAssembly)); ???????}) ???????.AddOperationalStore(options => ???????{ ???????????options.ConfigureDbContext = builder => ???????????????builder.UseMySQL(connectionString, ???????????????sql => sql.MigrationsAssembly(migrationsAssembly)); ???????????// this enables automatic token cleanup. this is optional. ???????????options.EnableTokenCleanup = true; ???????????options.TokenCleanupInterval = 30; ???????}) ???????.AddAspNetIdentity<ApplicationUser>();

添加迁移

在项目目录中打开命令提示符。在命令提示符下运行以下两个命令,一个是为了IdentityServer的配置,另一个是为了持久化授权:

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDbdotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

特别注意事项:使用 MariaDBMySQL 数据库时,执行完第一个命令之后,在 VS 中打开 Data\Migrations\IdentityServer\PersistedGrantDb\PersistedGrantDbContextModelSnapshot.cs 文件,找到大概在 32 - 34 行的如以下内容:

 ???????????????????b.Property<string>("Data") ???????????????????????.IsRequired() ???????????????????????.HasMaxLength(50000);

HasMaxLength(50000) 内的数字更改为HasMaxLength(20000) ,因为原来的长度 50000 超过了MySQL的字段长度。
修改完并保存后再执行第二个命令。
修改后的内容看起来象下面这个样子:

 ???????????????????b.Property<string>("Data") ???????????????????????.IsRequired() ???????????????????????.HasMaxLength(20000);

初始化数据库

进行了迁移后,可以编写代码来从迁移中创建数据库。还可以将之前定义的内存配置数据来为数据库设定种子。在 Startup.cs 中添加一个用来进行初始化数据库的方法:

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.GetClients()) ???????????{ ???????????????context.Clients.Add(client.ToEntity()); ???????????} ???????????context.SaveChanges(); ???????} ???????if (!context.IdentityResources.Any()) ???????{ ???????????foreach (var resource in Config.GetIdentityResources()) ???????????{ ???????????????context.IdentityResources.Add(resource.ToEntity()); ???????????} ???????????context.SaveChanges(); ???????} ???????if (!context.ApiResources.Any()) ???????{ ???????????foreach (var resource in Config.GetApiResources()) ???????????{ ???????????????context.ApiResources.Add(resource.ToEntity()); ???????????} ???????????context.SaveChanges(); ???????} ???}}

Configure方法调用它:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){ ???// this will do the initial DB population ???InitializeDatabase(app); ???// the rest of the code that was already here ???// ...}

再次运行程序,就可以将数据保存到数据库中了,我们可以通过数据库的客户端打开并看到写入到里面的内容:

  • InitializeDatabase方法并不适合每次运行应用程序时执行,填充数据库后,请考虑删除(或注释)对它的调用。

使用 ASP.NET Core Identity 的 IdentityServer4 授权服务器(二)

原文地址:https://www.cnblogs.com/mahidol/p/9362673.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved