Skip to content

Commit

Permalink
修复安全 KEY 加载时间不对的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
zlzforever committed Jul 26, 2024
1 parent 33dd64c commit 5bdd785
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/SecurityTokenService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
Expand All @@ -15,8 +15,14 @@ public class Program
{
public static void Main(string[] args)
{
var secret = "secret".Sha256();
Console.WriteLine($"Secret: {secret}");
if (args.Contains("--g-aes-key"))
{
using Aes aes = Aes.Create();
aes.KeySize = 128; // 可以设置为 128、192 或 256 位
aes.GenerateKey();
Console.WriteLine("生成的 AES 密钥: " + Convert.ToBase64String(aes.Key));
}

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

CreateHostBuilder(args).Build().Run();
Expand Down
17 changes: 8 additions & 9 deletions src/SecurityTokenService/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public void ConfigureServices(IServiceCollection services)
// {
// keysFolder.Create();
// }
// comments by lewis at 20240222
// 必须是 128、256 位

var dataProtectionKey = Configuration["DataProtection:Key"];
if (!string.IsNullOrEmpty(dataProtectionKey))
{
Util.DataProtectionKeyAes.Key = Encoding.UTF8.GetBytes(dataProtectionKey);
}

services.AddControllers();

Expand Down Expand Up @@ -93,15 +101,6 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// comments by lewis at 20240222
// 必须是 128、256 位

var dataProtectionKey = Configuration["DataProtection:Key"];
if (!string.IsNullOrEmpty(dataProtectionKey))
{
Util.DataProtectionKeyAes.Key = Encoding.UTF8.GetBytes(dataProtectionKey);
}

var logger = app.ApplicationServices.GetRequiredService<ILoggerFactory>().CreateLogger("Startup");
IdentitySeedData.Load(app);

Expand Down
2 changes: 1 addition & 1 deletion src/SecurityTokenService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
}
},
"DataProtection": {
"Key": "yD7wZi7jefkVwLM5"
"Key": ""
},
"Aliyun": {
"AccessKey": "",
Expand Down

0 comments on commit 5bdd785

Please sign in to comment.