






















环境: .netcore3.1
应用负载:10.5.45.104 指向 10.5.84.10,10.5.84.11两台服务器
负载轮询
10.5.84.10,10.5.84.11 上都部署有 ids4服务。
当前端通过负载获取到ids4 的 token 后,再次通过负载进行鉴权。就有可能找到另外一台服务器,从而鉴权失败。
解决方案:
🚨 所有 IdentityServer 节点必须使用同一套签名证书
这样 使token 无论找到哪台服务器都应该鉴权通过。
1. 生成证书
使用管理员权限 打开Power shell
执行
New-SelfSignedCertificate `
-Subject "CN=idsrv" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyExportPolicy Exportable
这样会生成一个key
Thumbprint Subject
---------- -------
FA26780436B953E841C6BF4AA8496EB3C96C47FB CN=idsrv
把它记录下来
也可以通过以下命令查看
Get-ChildItem Cert:\CurrentUser\My
保存下来,执行命令
$pwd = ConvertTo-SecureString -String "123456" -Force -AsPlainText
Export-PfxCertificate -Cert "Cert:\CurrentUser\My\FA26780436B953E841C6BF4AA8496EB3C96C47FB" -FilePath C:\idsrv.pfx -Password $pwd
把C:\idsrv.pfx 文件放在项目中,修改属性,始终复制到输出目录,这样发布后的文件夹就有这个文件了。
项目中的引用
在 *.HttpApi.Host 项目中,ConfigureServices()方法 里面
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Hosting;
public override void ConfigureServices(ServiceConfigurationContext context)
{
var env = context.Services.GetHostingEnvironment();
var certDir = Path.Combine(env.ContentRootPath, "certs");
var certPath = Path.Combine(certDir, "idsrv.pfx");
// ===== 1️⃣ 检查目录 =====
if (!Directory.Exists(certDir))
{
throw new Exception($"证书目录不存在: {certDir}");
}
// ===== 2️⃣ 检查文件 =====
// ===== 3️⃣ 加载证书 =====
if (File.Exists(certPath))
{
var certificate = new X509Certificate2(
certPath,
"123456",
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable
);
identityServerBuilder.AddSigningCredential(certificate);
}
else
{
// 仅开发环境 fallback
identityServerBuilder.AddAbpDeveloperSigningCredential();
}
context.Services
.AddIdentityServer()
.AddSigningCredential(certificate);
}
修改dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
MAINTAINER lihongyuan
LABEL description="this is a test website"
LABEL version="1.0"
WORKDIR /app
# 复制程序
COPY . .
# ⭐ 确保证书权限正确(关键)
RUN chmod 644 /app/certs/idsrv.pfx || true
EXPOSE 80
ENTRYPOINT ["dotnet", "RailCDE.HttpApi.Host.dll"]
访问测试
http://10.5.45.104:3147/railback/.well-known/openid-configuration/jwks
end.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。