gpt4 book ai didi

c# - 如何从 Azure 应用程序配置获取 .crt 和 .key 文件

转载 作者:行者123 更新时间:2023-12-03 05:11:25 42 4
gpt4 key购买 nike

我有一个 key 保管库,用于存储一个证书。目前,我使用 CSI 驱动程序从 key 保管库获取它并将其安装在我的 Pod 中。它显示如下:certcert.crtcert.key。我想从现在开始使用 Azure 应用程序配置。因此,我创建了对此证书的引用并尝试安装它,但我只得到一个文件证书。有没有办法同时获取 .crt.key 文件?我需要在同一 Pod 中的 .NET 应用程序中使用它们。

我尝试以不同的方式添加引用,并且我也在文档中搜索信息,但没有找到有关 Azure 应用程序配置和存储证书引用的任何信息。

最佳答案

我假设您正在使用 App Configuration .NET provider library ,旨在获取 Key Vault secret 或证书的 secret 值。对于 secret 和证书,您的应用程序的 IConfiguration 填充有 KeyVaultSecret.Value属性(property)。对于 Key Vault 证书,此 secret 值已包含证书的公钥和私钥,但需要解析此值才能获取单独的公钥/私钥。为了让您有所了解,Key Vault 中的此代码示例展示了如何解析 secret 值: Get a Certificate Including the Private Key .

private static X509Certificate2 ParseCertificate(KeyVaultSecret secret)
{
if (string.Equals(secret.Properties.ContentType, CertificateContentType.Pkcs12.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
byte[] pfx = Convert.FromBase64String(secret.Value);
return new X509Certificate2(pfx);
}

// For PEM, you'll need to extract the base64-encoded message body.
// .NET 5.0 introduces the System.Security.Cryptography.PemEncoding class to make this easier.
if (string.Equals(secret.Properties.ContentType, CertificateContentType.Pem.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
...
...

如您所见,解析逻辑还依赖于 KeyVaultSecret.Properties.ContentType,如果您使用应用程序配置提供程序库,则该解析逻辑不可用。

因此,最好的选择是使用 App Configuration .NET SDK 解析证书引用。此库返回您存储在应用程序配置中的原始 KeyVault 证书引用,您可以自行解析该引用:

Response<ConfigurationSetting> response = client.GetConfigurationSetting("MyCertificateReference");
if (response.Value is SecretReferenceConfigurationSetting secretReference)
{
var identifier = new KeyVaultCertificateIdentifier(secretReference.SecretId);
var certificateClient = new CertificateClient(identifier.VaultUri, new DefaultAzureCredential());
var cert = await certificateClient.GetCertificateAsync(identifier.Name);
}

关于c# - 如何从 Azure 应用程序配置获取 .crt 和 .key 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76589597/

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