ASP.NET核心OpenIdConnectServer数据保护密钥位置



你好,

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

但有一件事我无法解决——如何设置用于持久化令牌签名密钥的自定义文件夹?

在服务配置中,我有:

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

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

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

尽管如此,凭证签名密钥是在默认位置创建的:

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

那是bug还是功能?如何强制服务器将密钥也存储在keys/文件夹中?


摘要-我为什么这么做

我的想法是从ndocker镜像构建一个API,将其隐藏在负载均衡器后面,并在云中的某个地方运行。问题是,当docker中的每个实例都创建了自己的应用程序&签名密钥,加密的auth令牌将不适用于任何其他实例,除了使用其密钥创建和签名令牌的实例。因此,我试图将相同的密钥分配给每个正在运行的docker映像。如果可能的话,保存到预定义的应用程序文件夹。

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


提前谢谢你。

好吧,我想明白了。

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

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>"
}

启动.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());
    ...
});

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

但这是有效的。

问候

最新更新