Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException

Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException

我在 ASP.NET 核心 MVC 样板模板(DotNet 核心)的第一个 运行 上收到来自 Visual Studio 2017 的以下关于 SSL 证书的错误消息:

"Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException occurred HResult=0x80070002 Message=The system cannot find the file specified Source=
StackTrace: at Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData, String fileName, String password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) at Microsoft.AspNetCore.Hosting.KestrelServerOptionsHttpsExtensions.UseHttps(KestrelServerOptions options, String fileName, String password) ... "

使用 SSL 的所有其他项目工作正常,我已经仔细检查了我的本地主机证书是否在我本地计算机的受信任的根证书颁发机构中并且没有过期。 该项目是 运行ning IISExpress - 也许它没有找到正确的位置?我不确定。我哪里出错了?

正在发生两个问题之一。

1) 文件"exists",但是是一个符号链接。这往往会混淆底层系统。 (响应是执行 File.ReadAllBytes 并使用 byte[] 构造函数)。

2) 文件不存在。

为了帮助诊断 #2,您可以阅读 Environment.CurrentDirectory 以了解 "here" 的位置,并使用 Directory.EnumerateFiles() 查看目录中存在的内容以及您的文件不存在的原因存在。


当然,如果您认为您不是按文件加载,而是认为您是从证书存储加载:检查您的配置并重试...因为您是从文件加载:)。

最近 ASP.NET 核心 MVC 样板也有同样的问题。

关闭Visual Studio,右击它,"Run as Administrator"。为我工作。

如果您 运行 在 docker,另一个解决方法是在启动时进行复制。

# The copy is done, because wildcard_certificate.pfx is put into the container using docker secrets, which makes it a symlink. 
# Reading a certificate as a symlink is not supported at this moment: 
# After doing a copy, the copied version is not a symlink anymore.
ENTRYPOINT (IF EXIST "c:\certificates\wildcard_certificate.pfx" (copy c:\certificates\wildcard_certificate.pfx c:\app\wildcard_certificate.pfx)) && dotnet webapplication.dll

我的应用程序在 "c:\app" 文件夹中运行,我将 "to be copied" 证书放在 "c:\certificates" 中。在启动时,证书被复制到我的环境变量指向的"c:\app"。

version: "3.7"
services:
  webapplication:
    image: ({CONTAINER_REGISTRY})/webapplication:({LABEL})
    environment:
      - ASPNETCORE_URLS=https://+;http://+
      - ASPNETCORE_HTTPS_PORT=443
      - ASPNETCORE_Kestrel__Certificates__Default__Path=wildcard_certificate.pfx
    secrets:
      - source: config_secrets
        target: C:/app/appsettings.json
      - source: wildcard_certificate_pfx
        target: c:\certificates\wildcard_certificate.pfx

上下文

我正在设置一个新的 dotnet 6 mvcapp 网站并尝试在本地安装 Certbot 生成的证书。 kestrel 从文件加载证书的设置非常简单,但我不断遇到上述错误。 就像有人提到的那样,fullchain.pem 实际上是一个符号链接或换句话说:一个快捷方式。 解决方案是右键单击“.pem 文件”快捷方式和 select Open file location,然后获取实际的 .pem 文件。

正在更新您的设置以指向实际文件,您应该没问题。

指向活动文件夹的路径错误

"Certificates": {
  "Default": {
    "Path": "C:\Certbot\live\{my domain}\fullchain.pem",
    "KeyPath": "C:\Certbot\live\{my domain}\privkey.pem"
  }
}

正确的路径指向存档文件夹

"Certificates": {
  "Default": {
    "Path": "C:\Certbot\archive\{my domain}\fullchain.pem",
    "KeyPath": "C:\Certbot\archive\{my domain}\privkey.pem"
  }
}

我刚刚花了整个上午来修复这个错误....

我正在使用 Azure.Security.KeyVault.Certificates nuget 包从 KeyVault 下载证书并将其用于 IdentityServer。

我最近为我们的项目更新了 nuget 包,因此重构了一些代码,删除了拼图的关键部分。

 var signingCertificate = certificateClient.DownloadCertificate(new DownloadCertificateOptions(signingCertificateName)
 {
      KeyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.MachineKeySet,
 });

X509KeyStorageFlags.MachineKeySet 是在 Azure 上获得它 运行 的秘诀。在回到这个之前,我尝试了其他几种选择。烦人的是,在我开始重构之前就已经有了这个!