簡體   English   中英

(IdentityServer4) 如何在 Docker 容器中運行的 Asp.Net Core 應用程序中加載 X509 證書

[英](IdentityServer4) How Do I Load A X509 Certificate Inside An Asp.Net Core Application Running In A Docker Container

我需要在 asp.net 核心應用程序的 Startup() class 中加載 IdentityServer4 的簽名證書。

我將作為 docker 容器部署到 GKE 上現有的 Kubernetes 集群。

我不確定通過 IdentityServer asp.net 啟動 class 生成( cert-manager? )、存儲和訪問證書的最佳方法。

如果我能弄清楚基礎知識,我就可以學習如何部署和輪換/過期密鑰。

我認為你可以通過幾種方式做到這一點。 就個人而言,我選擇將此憑證存儲在Secret中。 我正在使用一個我沒有生成的.pfx 文件,因此您必須對該部分進行分類。

生成證書並將其加載到密鑰后,使用:

kubectl create secret generic <secret name> --from-file=<path to file>

在您的部署中。yaml 為您的容器配置一個卷:

spec:
  volumes:
  - name: secrets-volume
    secret:
      secretName: <secret name>
  containers:
    volumeMounts:
    - name: secrets-volume
      mountPath: app/secrets
      readOnly: true

最后,在您的 Startup.cs 中,您可以添加與此類似的內容:

X509Certificate2 rsaCertificate = null;
try
{
     rsaCertificate = new X509Certificate2(
          Path.Combine(Environment.ContentRootPath, "secrets/certificate.pfx"),
          "password!123"
          );
}
catch (System.Exception)
{
    // logs
}

if (Environment.IsDevelopment())
{
    builder.AddDeveloperSigningCredential();
}
else
{
    if (rsaCertificate == null)
    {
         throw new System.Exception("Signing Credential not found");
    }

    builder.AddSigningCredential(rsaCertificate);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM