【问题标题】:Read a TLS/SSL private key certificate in your code in Azure App Service在 Azure 应用服务的代码中读取 TLS/SSL 私钥证书
【发布时间】:2020-08-13 16:08:37
【问题描述】:

在此 Microsoft article 之后,我们正在访问上传到我们的应用服务的代码中的私钥证书。正如文章所示,我们可以通过其指纹查找证书,并且可以读取它,但是当我们尝试使用以下代码访问其密钥值时,我们会收到错误“CryptographicException:密钥集不存在。”。将密钥转换为 RSACryptoServiceProvider 时发生错误。

此错误消息具有欺骗性,因为我知道密钥集确实存在。在我的非 Azure 开发 VM 上,我可以像任何其他证书一样将私钥文件加载到我的本地计算机证书存储中,并使用相同的代码读取它们的密钥及其值。我确实必须向我的站点在本地运行的用户(IIS 应用程序池标识)授予对证书密钥的访问权限。您可以在 Microsoft 管理控制台中执行此操作(参见图片)。因此我们推测这是一个授权问题。

有没有其他人遇到过这种情况?

        // private key
        if (certificate.HasPrivateKey)
        {
            try
            {
                rsa = certificate.PrivateKey as RSACryptoServiceProvider;
            }
            catch (Exception e)
            {
                var message = $"Certificate key cast to RSACryptoServiceProvider: {e.ToString()} Stacktrace: {e.StackTrace}";
                Error.LogError(message);
                throw new Exception(message);
            }
        }

        if (rsa is null)
        {
            var message = $"Certificate with thumbprint {certificate.Thumbprint} contains no private key.";
            Error.LogError(message);
            throw new Exception(message);
        }

        // signiture
        var cspParam = new CspParameters
        {
            KeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName,
            KeyNumber = rsa.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2
        };

堆栈跟踪: 22488 21:52:18 错误证书密钥强制转换为 RSACryptoServiceProvider:System.Security.Cryptography.CryptographicException:密钥集不存在

在 System.Security.Cryptography.Utils.CreateProvHandle(CspParameters 参数,布尔 randomKeyContainer) 在 System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType,CspParameters 参数,布尔 randomKeyContainer,Int32 dwKeySize,SafeProvHandle&safeProvHandle,SafeKeyHandle&safeKeyHandle) 在 System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() 在 System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize,CspParameters 参数,布尔值 useDefaultKeySize) 在 System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() 在 BCU.Web.Controllers.CovidLiveAgentController.GetAccessToken() 堆栈跟踪:在 System.Security.Cryptography.Utils.CreateProvHandle(CspParameters 参数,布尔 randomKeyContainer) 在 System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType,CspParameters 参数,布尔 randomKeyContainer,Int32 dwKeySize,SafeProvHandle&safeProvHandle,SafeKeyHandle&safeKeyHandle) 在 System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() 在 System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize,CspParameters 参数,布尔值 useDefaultKeySize) 在 System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() 在 BCU.Web.Controllers.CovidLiveAgentController.GetAccessToken()

【问题讨论】:

  • 显示您从商店中检索证书的代码。

标签: azure x509certificate private-key azure-appservice cryptographicexception


【解决方案1】:

首先,永远不要碰X509Certificate2.PrivateKey 属性。如果您在 .NET Framework 4.6+ 中,它已过时且不应使用。相反,您需要使用正确的extension method 来根据私钥算法检索私钥对象。更多关于为什么PrivateKey属性不好的细节在我的博文中:https://www.pkisolutions.com/accessing-and-using-certificate-private-keys-in-net-framework-net-core/

如果密钥存储在本地机器存储中(而不是当前用户),则执行应用程序的用户帐户必须被授予私钥读取权限。在给定的情况下,您应该将证书加载到 Current User\Personal 存储,而不是 referenced article 中指定的 Local Machine\Personal。这消除了关键权限授予要求

【讨论】:

  • 谢谢!我将使用各种扩展方法进行测试。还有一些附加信息 - 这是一个不寻常的情况,请参阅重新引用的 Microsoft atricle,此 .pfx 证书已上传到我们的 Azure 应用服务的私钥集合中。所以没有办法选择它进入哪个商店或授予个人用户权限。
  • 即使使用扩展方法,我们似乎仍然遇到同样的问题。我们一直在调整我们的 Azure 订阅和实例计数,奇怪的是似乎解决了这个问题,但只是间歇性的。当我们重新启动应用服务时,第一次尝试成功,但随后对相同代码的任何调用都会失败。
猜你喜欢
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
相关资源
最近更新 更多