gpt4 book ai didi

c# - ASP.NET Core OpenIdConnect 服务器数据保护 key 位置

转载 作者:太空狗 更新时间:2023-10-29 23:20:43 25 4
gpt4 key购买 nike

你好,

在我的 ASP.NET Core 应用程序中,我使用 OpenIdConnectServer 进行 Api 身份验证。一切正常。

但有一件事我无法解决 - 如何设置自定义文件夹以保留 token 签名 key ?

在服务配置中我有:

services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"keys/"));

首次运行后,会在那里创建一个应用程序 key 。但是 OpenIdServer 以某种方式自行管理 key 。

app.UseOpenIdConnectServer(options => {
...
options.DataProtectionProvider = app.ApplicationServices.GetDataProtectionProvider();
...
});

尽管如此,凭据签名 key 是在默认位置创建的:

 A new RSA key ... persisted on the disk: /home/.../.aspnet/aspnet-contrib/oidc-server/<some guid>.key.

这是错误还是功能?如何强制服务器将 key 也存储在 keys/ 文件夹中?


摘要 - 为什么我要这样做:

我的想法是根据 n 个 docker 镜像构建一个 API,将其隐藏在负载均衡器后面并在云中的某个地方运行。问题是 - 当 docker 中的每个实例都创建它自己的应用程序和签名 key 时,加密的身份验证 token 将不适用于任何其他实例,除了已经创建并使用其 key 签署 token 的实例。因此,我试图将相同的 key 分发给每个正在运行的 docker 镜像。如果可能,到预定义的应用程序文件夹。

或者是否有更好的方法或最佳实践?


提前致谢。

最佳答案

嗯,我想通了。

首先,我们必须生成 x509 证书(带有私钥),如 here 所述

openssl genrsa -out private.key 1024
openssl req -new -x509 -key private.key -out publickey.cer -days 365
openssl pkcs12 -export -out certificate.pfx -inkey private.key -in publickey.cer

将其复制到您喜欢的文件夹,在本例中为 key/certificate.pfx

然后,轻轻地将您的新证书插入 OpenIdConnectServer:

appsettings.json

"Keys": {
"CertificatePath": "keys/certificate.pfx",
"CertificatePassword": "<password you provider>"
}

Startup.cs

private X509Certificate2 CreateOauthCertificate(){
var path = Configuration["Keys:CertificatePath"];
var password = Configuration["Keys:CertificatePassword"];
return new X509Certificate2(path, password);
}

Starup.cs - 配置

app.UseOpenIdConnectServer(
...
options.SigningCredentials.AddCertificate(CreateOauthCertificate());
...
});

现在我很好奇是否有更好的方法。

但这行得通。

问候

关于c# - ASP.NET Core OpenIdConnect 服务器数据保护 key 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com