Skip to content

Commit

Permalink
先生成 XML KEY 表
Browse files Browse the repository at this point in the history
  • Loading branch information
zlzforever committed Aug 13, 2024
1 parent dab1afc commit d62472a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
15 changes: 4 additions & 11 deletions src/SecurityTokenService/Data/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@

namespace SecurityTokenService.Data;

public class SeedData
public class SeedData(UserManager<User> userManager)
{
private readonly UserManager<User> _userManager;

public SeedData(UserManager<User> 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)
Expand All @@ -32,4 +25,4 @@ public void Load()
}
}
}
}
}
29 changes: 2 additions & 27 deletions src/SecurityTokenService/Identity/IdentitySeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,19 @@ public static void Load(IApplicationBuilder app)
{
using var scope = app.ApplicationServices.CreateScope();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
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<MySqlSecurityTokenServiceDbContext>();
}
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<PostgreSqlSecurityTokenServiceDbContext>();
}

conn.Dispose();

if (string.Equals(configuration["Identity:SelfHost"], "true", StringComparison.InvariantCultureIgnoreCase))
{
securityTokenServiceDbContext.Database.Migrate();
Expand All @@ -71,4 +46,4 @@ xml varchar(2000) not null
seedData.Load();
securityTokenServiceDbContext.Dispose();
}
}
}
33 changes: 33 additions & 0 deletions src/SecurityTokenService/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d62472a

Please sign in to comment.