From d62472af297a70bcb4c7fcc297134459e51456b6 Mon Sep 17 00:00:00 2001 From: Lewis Zou Date: Tue, 13 Aug 2024 14:03:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E7=94=9F=E6=88=90=20XML=20KEY=20?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SecurityTokenService/Data/SeedData.cs | 15 +++------ .../Identity/IdentitySeedData.cs | 29 ++-------------- .../WebApplicationBuilderExtensions.cs | 33 +++++++++++++++++++ 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/SecurityTokenService/Data/SeedData.cs b/src/SecurityTokenService/Data/SeedData.cs index 391985c..1ca11b2 100644 --- a/src/SecurityTokenService/Data/SeedData.cs +++ b/src/SecurityTokenService/Data/SeedData.cs @@ -6,22 +6,15 @@ namespace SecurityTokenService.Data; -public class SeedData +public class SeedData(UserManager userManager) { - private readonly UserManager _userManager; - - public SeedData(UserManager userManager) - { - _userManager = userManager; - } - public void Load() { - if (!_userManager.Users.Any()) + if (!userManager.Users.Any()) { foreach (var user in TestUsers.Users) { - var result = _userManager + var result = userManager .CreateAsync(new User(user.Username), user.Password) .Result; if (!result.Succeeded) @@ -32,4 +25,4 @@ public void Load() } } } -} \ No newline at end of file +} diff --git a/src/SecurityTokenService/Identity/IdentitySeedData.cs b/src/SecurityTokenService/Identity/IdentitySeedData.cs index 1396063..6284666 100644 --- a/src/SecurityTokenService/Identity/IdentitySeedData.cs +++ b/src/SecurityTokenService/Identity/IdentitySeedData.cs @@ -21,44 +21,19 @@ public static void Load(IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); var configuration = scope.ServiceProvider.GetRequiredService(); - var connectionString = configuration["ConnectionStrings:Identity"]; DbContext securityTokenServiceDbContext; - DbConnection conn; + if (configuration.GetDatabaseType() == "MySql") { - conn = new MySqlConnection(connectionString); - conn.Execute($""" - create table if not exists system_data_protection_keys - ( - id int auto_increment primary key, - friendly_name varchar(64) not null, - xml varchar(2000) not null - ); - """ - ); - securityTokenServiceDbContext = scope.ServiceProvider.GetRequiredService(); } else { - conn = new NpgsqlConnection(connectionString); - conn.Execute($""" - create table if not exists system_data_protection_keys - ( - id serial primary key, - friendly_name varchar(64) not null, - xml varchar(2000) not null - ); - """ - ); - securityTokenServiceDbContext = scope.ServiceProvider.GetRequiredService(); } - conn.Dispose(); - if (string.Equals(configuration["Identity:SelfHost"], "true", StringComparison.InvariantCultureIgnoreCase)) { securityTokenServiceDbContext.Database.Migrate(); @@ -71,4 +46,4 @@ xml varchar(2000) not null seedData.Load(); securityTokenServiceDbContext.Dispose(); } -} \ No newline at end of file +} diff --git a/src/SecurityTokenService/WebApplicationBuilderExtensions.cs b/src/SecurityTokenService/WebApplicationBuilderExtensions.cs index 9cac9fc..2610aa3 100644 --- a/src/SecurityTokenService/WebApplicationBuilderExtensions.cs +++ b/src/SecurityTokenService/WebApplicationBuilderExtensions.cs @@ -1,9 +1,11 @@ using System; +using System.Data.Common; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Intrinsics.Arm; using System.Text; +using Dapper; using IdentityServer4; using IdentityServer4.Configuration; using Microsoft.AspNetCore.Authentication.Cookies; @@ -13,6 +15,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using MySqlConnector; +using Npgsql; using SecurityTokenService.Data.MySql; using SecurityTokenService.Data.PostgreSql; using SecurityTokenService.Extensions; @@ -130,6 +134,35 @@ public static WebApplicationBuilder AddDataProtection(this WebApplicationBuilder } } + var connectionString = builder.Configuration.GetConnectionString("Identity"); + + if (builder.Configuration.GetDatabaseType() == "MySql") + { + using var conn = new MySqlConnection(connectionString); + conn.Execute( + $""" + create table if not exists system_data_protection_keys + ( + id int auto_increment primary key, + friendly_name varchar(64) not null, + xml varchar(2000) not null + ); + """ + ); + } + else + { + using var conn = new NpgsqlConnection(connectionString); + conn.Execute($""" + create table if not exists system_data_protection_keys + ( + id int auto_increment primary key, + friendly_name varchar(64) not null, + xml varchar(2000) not null + ); + """); + } + // 影响隐私数据加密、AntiToken 加解密 var dataProtectionBuilder = builder.Services.AddDataProtection() .SetApplicationName("SecurityTokenService")