从 Azure KeyVault 加载 X509Certificate

Loading X509Certificate from Azure KeyVault

我们有一个托管在 Azure 中的 Web 应用程序,我们使用它从 KeyVault 加载证书以用于加密和其他服务的身份验证。从 KeyVault 读取值后,我们将创建如下证书:

return new X509Certificate2(Convert.FromBase64String(secret.Value), string.Empty,
                    X509KeyStorageFlags.MachineKeySet |
                    X509KeyStorageFlags.PersistKeySet |
                    X509KeyStorageFlags.Exportable);

我们发现了很多关于 "Keyset does not exist" 和 "The request was aborted: Could not create SSL/TLS secure channel" 的错误。

我怀疑这是因为它将私钥保存到磁盘 (C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys),这在尝试再次读取它时会导致问题,因为它是一个共享的托管环境,而我们不这样做完全拥有机器。

有没有正确的方法来做到这一点?我看到过使用证书存储的参考资料,但我不太确定这与从 KeyVault 获取证书有何关系。

Is there a proper way to do this? I've seen references to using the certificate store, but I'm not too sure how that ties in with getting the certificate from KeyVault.

在您的情况下,您似乎只是从 keyvault 加载了证书,但证书并未部署到 Azure WebApp。关于如何通过 Key Vault 部署 Azure Web App Certificate,您可以从这里获得详细步骤 blog.

在 Azure WebApp 中使用证书的最简单方法是您可以从 Azure 门户 upload the pfx file to the Azure WebApp 添加一个名为 WEBSITE_LOAD_CERTIFICATES 的应用程序设置并设置它对证书指纹的价值。 但是和KeyVault没有关系

我认为更好的解决方案是在适用的情况下使用 Managed Service Identity(MSI)。 MSI 功能将在 Azure AD 中创建一个服务主体来表示 service/VM 的身份。并在密钥保管库中分配此服务主体 属性 角色或权限,这可以通过在密钥保管库的 "Setting -> Access policies" 部分中创建新的访问策略来完成。服务主体应与服务名称相同。如果通过 powershell 创建,此名称可以不同。

MSI 设置后,可以将证书或机密上传到密钥库并在服务中访问。