【问题标题】:Azure web app sporadically throws CryptographicException: Keyset does not existAzure Web 应用偶尔抛出 CryptographicException:密钥集不存在
【发布时间】:2016-09-15 14:58:24
【问题描述】:

我在 Azure 上托管了一个 Identity Server Web 应用程序。它的根目录中有一个.pfx 文件用于签名。问题是,当新发布时它工作得很好,但过了一段时间它开始抛出CryptographicException: Keyset does not exist.

基于CryptographicException KeySet does not exists,我认为这是一个文件访问问题,但为什么突然天蓝色会搞乱文件访问。

【问题讨论】:

  • 我有类似的问题。我的猜测是它可能连接到数据保护密钥,但还不确定。

标签: c# asp.net azure identityserver4


【解决方案1】:

我偶尔会看到同样的异常。就我而言,这是由于我在部署槽之间交换时数据保护密钥发生变化引起的。将 services.AddDataProtection() 与默认配置一起使用并在 Azure 应用服务上托管应用时,数据保护密钥存储在 %HOME%\ASP.NET\DataProtection-keys 中。此目录由网络共享支持,以确保密钥在您的所有应用程序实例上都可用,但是部署槽并非如此。因此,当您从一个部署槽切换到另一个部署槽时,密钥会发生变化,当您的应用尝试取消对有效负载的保护时,您会看到 CryptographicException: Keyset does not exist

为确保您的应用始终使用相同的密钥,您需要配置不同的密钥存储提供程序。 IE。使用 Redis 或 Azure Blob 存储作为后备存储。

您可以在official documentation 中找到有关如何配置此功能的更多信息。

我还描述了我在my blog 上遇到的问题。

【讨论】:

    【解决方案2】:

    我建议您不要先从磁盘中检索您的签名证书。而是将证书上传到 Azure 门户中的关联 Web 应用程序。

    并在应用程序启动时检索证书

    类似的东西。

    X509Certificate2 Certificate = null;
    var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    certStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
    var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint,"CERTIFICATE_THUMBPRINT_HERE",false);
    Certificate = certCollection[0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 2017-06-23
      • 2014-09-14
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多