繁体   English   中英

.NET 核心应用在生产中找不到上传的证书 [Azure Key Vault]

[英].NET Core app can't find uploaded certificate in production [Azure Key Vault]

我最近阅读了很多关于使用 Azure Key Vault 管理机密的内容。 我设法使用 Ubuntu 20.04 在服务器中创建和安装 a.pfx 证书,并按照这些步骤将证书上传到我的 Azure AD。

在连接到我的 Key Vault 之前正确找到了证书,并且在我从 Windows 和 Linux (WSL) 开发时检索了机密。 但是,当我将应用程序部署到我的生产服务器时,我创建的用于管理 kestrel 的服务会引发“core-dump”错误,类似于这个问题

但就我而言,当我查看日记时,我发现以下内容:

Unhandled exception. System.InvalidOperationException: Sequence contains no elements

令人惊讶的是,如果我只是使用“dotnet app.dll”手动运行应用程序,这不会发生。

这怎么可能? 如果我手动运行它,它会打开商店,找到证书并访问机密,但在服务运行时找不到任何东西。

这是我用来在我的 Program.cs 中配置对 Key Vault 的访问的相关代码:

// Azure Key Vault configuration.
using var store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, configuration["KeyVault:AzureADCertThumbprint"], false);

configuration.AddAzureKeyVault(
    new Uri($"https://{configuration["KeyVault:KeyVaultName"]}.vault.azure.net/"),
    new ClientCertificateCredential(configuration["KeyVault:AzureADDirectoryId"], configuration["KeyVault:AzureADApplicationId"], certs.OfType<X509Certificate2>().Single()),
    new KeyVaultSecretManager());

store.Close();

谁能帮我找到问题? 提前致谢。

  • 我检查了一些 Microsoft 文档,但没有发现访问密钥库的代码有任何问题。 我猜您可能会忘记门户上的application setting中的某些内容,因为您的代码没有问题。

  • 创建证书后,配置 Azure AD 并关联证书,之后我们可以使用 X509 证书从 .NET 客户端访问 Azure Key Vault

  • 导入证书并将证书数据投射到 Base64 后,我们应该创建 Azure 资源管理器 AD 应用程序和服务主体。 为 Azure Key Vault 成功配置 Azure 资源管理器应用程序和服务主体可以解决您面临的问题。

在此处输入图像描述

检查此Accessing Azure Key Vaults Using Certification and Setup Key Vault using Azure AD Application and Certificates了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM