将 IdentityServer 4 部署到 Azure VM 时找不到证书

Certificate not found when deploying IdentityServer 4 to Azure VM

我在将 IdentityServer 4 (ASP.NET Core 1.1) 部署到 Azure VM (Windows Server 2012 R2) 时遇到此证书问题。

crit: IdentityServer4.Hosting.IdentityServerMiddleware[0]
      Unhandled exception: Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The system cannot find the file specified
         at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider, CngKeyOpenOptions openOptions)
         at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider)
         at Internal.Cryptography.Pal.CertificatePal.GetPrivateKey[T](Func`2 createCsp, Func`2 createCng)
         at Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey()

这将在本地开发计算机上运行,​​我不明白为什么。我在 VM 上的“我的(本地计算机)”商店上安装了证书。

谁能帮忙解决这个问题。谢谢

我在这里找到了解决方案:

http://www.daves-blog.net/post/2014/06/16/X509Certificate-The-System-cannot-find-the-file-specified.aspx

显然,我需要在应用程序池上启用加载用户配置文件。但是,错误现在更改为:

crit: IdentityServer4.Hosting.IdentityServerMiddleware[0]
  Unhandled exception: Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Keyset does not exist
     at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider, CngKeyOpenOptions openOptions)
     at System.Security.Cryptography.CngKey.Open(String keyName, CngProvider provider)
     at Internal.Cryptography.Pal.CertificatePal.GetPrivateKey[T](Func`2 createCsp, Func`2 createCng)
     at Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey()

现在查看问题。

好的,所以问题是读取证书的权限。我们所要做的只是去证书并授予 Read (only) for IIS_IUSRS.

现在可以了。

如果有人想知道如何让 IIS 用户访问证书

  1. 创建/购买证书。确保它有私钥。
  2. 将证书导入 "Local Computer" 帐户。最好使用证书 MMC。 确保勾选 "Allow private key to be exported"
  3. 基于此,IIS 7.5 应用程序池的标识使用以下之一。

    • IIS 7.5 网站在 ApplicationPoolIdentity 下 运行。打开 MMC => 添加证书(本地计算机)管理单元 => 证书(本地计算机)=> 个人 => 证书 => 右键单击​​感兴趣的证书 => 所有任务 => 管理私钥 => 添加 IIS AppPool\AppPoolName 并授予它 Full control。将“AppPoolName”替换为您的应用程序池的名称(有时 IIS_IUSRS
    • IIS 7.5 网站在 运行 NETWORK SERVICE 下。使用证书 MMC,在 "Local Computer\Personal".
    • 中将 "NETWORK SERVICE" 添加到对证书的完全信任
    • IIS 7.5 网站是 运行 在 "MyIISUser" 本地计算机用户帐户下。使用证书 MMC,将 "MyIISUser"(一个新的本地计算机用户帐户)添加到 "Local Computer\Personal" 中证书的完全信任。

以上摘自here.