【问题标题】:Unable to load a certificate in Azure Function无法在 Azure Function 中加载证书
【发布时间】:2019-02-02 09:27:10
【问题描述】:

我正在开发一个 Azure 函数,它需要从安全的 Base 64 字符串加载证书。证书受密钥保护。证书和密码存储在 Azure Key Vault 中。

当我尝试从函数加载证书时,我在 v1 和 v2 函数中都遇到错误。

这是用于加载证书的代码:

var certificate = new X509Certificate2(Convert.FromBase64String(certificateBase64), certificatePassword)

使用 .Net 的这段代码,我有一个奇怪的问题,我可以在本地重现。该问题与 .Net 4.6.1 相关联。在 .Net Core 2.0 中,它在本地运行良好(在 Azure Function CLI 中),但我遇到了一个关于找不到文件的奇怪问题(https://github.com/dotnet/corefx/issues/11042

正如上一篇文章末尾提到的,我尝试了 X509KeyStorageFlags.EphemeralKeySet 标志。

var certificate = new X509Certificate2(Convert.FromBase64String(certificateBase64), certificatePassword, X509KeyStorageFlags.EphemeralKeySet)

该标志尚不适用于 .Net Core 2 (https://github.com/dotnet/corefx/issues/24454),也不适用于 Azure Functions 使用的框架 .Net 4.6.1。

有没有办法强制 Azure Function v1 使用 Net Framework? .Net Core 2.0 中是否有任何简单解决方法,但不将证书作为文件存储在函数上?

【问题讨论】:

  • 您是否尝试过本文中概述的方法?它适用于应用服务,但功能也应如此:msftplayground.com/2016/11/…
  • 嗨,保罗。尝试了博文中描述的解决方案,但找不到证书。

标签: c# azure azure-functions x509certificate x509certificate2


【解决方案1】:

经过多次尝试,我找到了解决方案。必须使用标志 X509KeyStorageFlags.MachineKeySet。它适用于 Function App V1 (.Net 4.6.1) 和 Function App V2 (.Net Core 2):

var certificate = new X509Certificate2(Convert.FromBase64String(certificateBase64), certificatePassword, X509KeyStorageFlags.MachineKeySet)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 2019-02-12
    • 2017-03-12
    • 1970-01-01
    • 2022-07-16
    • 2021-12-04
    相关资源
    最近更新 更多