【问题标题】:Azure, App-service, create X509Certificate2 object from stringAzure,应用服务,从字符串创建 X509Certificate2 对象
【发布时间】:2017-07-20 20:41:04
【问题描述】:

在 Azure 中有一个应用服务,并在 AzureServiceManagementAPI 上工作,我正在下载包含每个订阅的管理证书的文件。

如何使用文件中的证书字符串我正在尝试创建一个 X509Certificate2 对象。

string cerStr = subscription.Attribute("ManagementCertificate").Value;
X509Certificate2 x509 = new X509Certificate2(Convert.FromBase64String(cerStr), string.Empty, X509KeyStorageFlags.MachineKeySet)

X509Certificate2的构造函数抛出异常

访问被拒绝。

System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 小时)在 System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(字节[] rawData, IntPtr 密码, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle & pCertCtx) 在 System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(字节[] rawData, 对象密码, X509KeyStorageFlags keyStorageFlags)

【问题讨论】:

  • 你试过 X509KeyStorageFlags.UserKeySet 吗?

标签: c# azure azure-web-app-service x509certificate x509certificate2


【解决方案1】:

由于没有人回答过这个问题,我会尝试着去做。如果我错了,请纠正我,但我认为问题是以下代码行:

new X509Certificate2(Convert.FromBase64String(cerStr), string.Empty, X509KeyStorageFlags.MachineKeySet) 

这行代码将尝试将新证书添加到虚拟机的证书存储中。运行时使用的所有证书都需要托管在某处的商店中。这不是一个好主意,因为托管应用服务的虚拟机的证书存储区不是您应该在其中存储任何内容的东西,它是基础架构的一部分,当您使用应用服务时,您无需担心。

您需要做的是通过 azure 门户上传证书(如果它们不存在)。为此,我最终重用了已经存在的 SSL 证书。完成后,您可以在代码中检索该证书。您需要在 Azure 门户中的“应用程序设置”键下为您的应用服务添加一个名为 WEBSITE_LOAD_CERTIFICATES 的新应用设置。该值应该是证书的指纹。

要检索证书,您应该执行以下操作:

public async Task<X509Certificate2> GetCertificate(string certificateThumbprint)
{
    var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadOnly);
    var cert = store.Certificates.OfType<X509Certificate2>()
        .FirstOrDefault(x => x.Thumbprint == certificateThumbprint);
    store.Close();
    return cert;
}

您可以通过使用 azure 资源浏览器https://resources.azure.com/ 导航订阅来获取证书的指纹

【讨论】:

  • 根据我的经验,证书将安装在 CurrentUser 中。
  • 这周围没有吗?我们中的一些人从外部服务加载证书。
【解决方案2】:

正如 Fredrik 所说,问题是由代码引起的

X509Certificate2 x509 = new X509Certificate2(Convert.FromBase64String(cerStr), string.Empty, X509KeyStorageFlags.MachineKeySet)

在 Azure WebApp 中,如果我们尝试使用证书,我们需要从 Azure 门户上传证书。在 Azure WebApp 应用程序中添加带有指纹值的 WEBSITE_LOAD_CERTIFICATES。更多详细信息请参考blog

Web应用访问证书,sn-p代码来自博客

    static void Main(string[] args)
    {
      X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
      certStore.Open(OpenFlags.ReadOnly);
      X509Certificate2Collection certCollection = certStore.Certificates.Find(
                                 X509FindType.FindByThumbprint,
                                 // Replace below with your cert's thumbprint
                                 “E661583E8FABEF4C0BEF694CBC41C28FB81CD870”,
                                 false);
      // Get the first cert with the thumbprint
      if (certCollection.Count > 0)
      {
        X509Certificate2 cert = certCollection[0];
        // Use certificate
        Console.WriteLine(cert.FriendlyName);
      }
      certStore.Close();
    }

【讨论】:

  • 这周围没有吗?我们中的一些人从外部服务加载证书。
猜你喜欢
  • 2016-08-30
  • 1970-01-01
  • 2021-12-07
  • 2015-09-08
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
相关资源
最近更新 更多