【问题标题】:IdentityServer4: How to load Signing Credential from Cert Store when in DockerIdentityServer4:如何在 Docker 中从 Cert Store 加载签名凭证
【发布时间】:2017-09-03 06:24:06
【问题描述】:

我们有一个基于 IdentityServer4 的 STS 在 Windows 上成功运行,其中签名凭据已安装到本地计算机,其中 .pfx 在 Personal > Certificates 下,.cer 在 Trusted People > Certificates 下。我们是 然后能够按其通用名称加载签名凭据,如下所示:

services.AddIdentityServer()
    .AddSigningCredential("CN=CERT_NAME")
    ...

我们现在想要在 Docker 容器中运行我们的 STS 实现,并且遇到了以下异常:

Unhandled Exception: System.PlatformNotSupportedException: Unix LocalMachine X509Store is limited to the Root and CertificateAuthority stores.
   at Internal.Cryptography.Pal.StorePal.FromSystemStore(String storeName, StoreLocation storeLocation, OpenFlags openFlags)
   at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags)
   at IdentityModel.X509CertificatesFinder.Find(Object findValue, Boolean validOnly)
   at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderExtensionsCrypto.AddSigningCredential(IIdentityServerBuilder builder, String name, StoreLocation location, NameType nameType)

根据上面的错误消息,以及我们在这里使用的 AddSigningCredential 方法的来源:https://github.com/IdentityServer/IdentityServer4/blob/ec17672d27f9bed42f9110d73755170ee9265116/src/IdentityServer4/Configuration/DependencyInjection/BuilderExtensions/Crypto.cs#L73,我们的问题似乎是 IdentityServer4 正在本地机器的个人(“我的” ) 存储,但是根据错误消息,这样的存储在 Unix 环境中不可用。

所以,我很想知道是否存在一些在 Docker 容器中加载 IdentityServer4 的签名凭据的最佳做法,如果无法通过名称或指纹加载它。唯一的选择是将证书与我们的应用程序捆绑在一起,然后按文件名加载吗?

【问题讨论】:

  • 嗨,肖恩,你解决了这个问题吗?
  • @xszaboj 看来是在 .NET Core 2 中实现的(至少对于 CurrentUser\My 来说),见github.com/aspnet/DataProtection/issues/125。这不是 LocalMachine\My 的情况
  • 嘿,我在 .NET CORE 3.1 中遇到此错误,有人有解决方案吗?

标签: docker x509 identityserver4


【解决方案1】:

当您使用 Docker 容器和 IdentityServer 时,基本上您有两种选择:

  • 将证书添加到容器映像 (COPY certificate.pfx .)
  • 将证书挂载到容器 (-v /path/to/certificate.pfx:/certificate.pfx)

无论您选择什么选项,您只需在Startup中的ConfigureServices中添加以下配置代码

var identityServerBuilder = services.AddIdentityServer();
/* store configuration and etc. is omitted */
if (_hostingEnvironment.IsDevelopment())
{
    identityServerBuilder.AddDeveloperSigningCredential();
}
else
{
    var certificate = new X509Certificate2("certificate.pfx", "certificate_password");
    identityServerBuilder.AddSigningCredential(certificate);
}

从配置、环境变量或机密存储中读取证书密码也是一个好主意。

【讨论】:

  • 我将我的应用托管在 linux azure 应用服务中。我直接从 Visual Studio 发布到 azure app 服务。如何复制或挂载证书?我仍然收到上面的代码错误。请帮忙
【解决方案2】:

我正在 windows 机器上开发,我使用以下代码从商店获取证书

X509Certificate2 cert = null;

X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            certStore.Open(OpenFlags.ReadOnly);

X509Certificate2Collection certCollection = certStore.Certificates.Find(
                        X509FindType.FindByThumbprint,
                        "‎thumbprint",
                        false);
if (certCollection.Count > 0)
    {
        cert = certCollection[0];
        Log.Logger.Information($"Successfully loaded cert from registry: {cert.Thumbprint}");

    }
    if (cert == null) // Fallback
    {
        cert = new X509Certificate2(Path.Combine(_env.ContentRootPath, "certificate.pfx"), "password");
        //Log.Logger.Information($"Falling back to cert from file. Successfully loaded: {cert.Thumbprint}");
    }
    else
    {
        certStore.Dispose();
    }

【讨论】:

  • 这个问题要求在 Docker 中,而不是在 Windows 中
  • 这个答案不适合这个问题
猜你喜欢
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2018-04-01
  • 2020-07-16
相关资源
最近更新 更多