繁体   English   中英

使用X509Certificate2加载pfx文件时出现“内部错误。”

[英]“An internal error occurred.” when loading pfx file with X509Certificate2

我正在尝试使用自签名证书(c#):

X509Certificate2 cert = new X509Certificate2(
    Server.MapPath("~/App_Data/myhost.pfx"), "pass");

在共享的Web托管服务器上,我收到一个错误:

System.Security.Cryptography.CryptographicException: An internal error occurred.

堆栈跟踪结束

System.Security.Cryptography.CryptographicException.
    ThrowCryptogaphicException(Int32 hr) +33
System.Security.Cryptography.X509Certificates.X509Utils.
    _LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, 
        Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.
    LoadCertificateFromFile(String fileName, Object password, 
        X509KeyStorageFlags keyStorageFlags) +237
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(
    String fileName, String password) +131

在我的开发机器上,它加载正常。 我加载* .pfx而不是* .cer文件的原因是因为我需要私钥访问(cer文件加载好)。 我在我的dev mochine上制作了pfx:

makecert -r -n "CN=myhost.com, E=admin@myhost.com" -sky exchange -b 01/01/2009
    -pe -sv myhost.pvk myhost.cer
<b>pvk2pfx</b> -pvk myhost.pvk -spc myhost.cer -pfx myhost.pfx -po pass</code>

我使用的是makecert的v5.131.3790.0版本

使用本地计算机商店作为私钥:

X509Certificate2 cert = new X509Certificate2("myhost.pfx", "pass",
    X509KeyStorageFlags.MachineKeySet);

MachineKeySet被描述为“私钥存储在本地计算机存储而不是当前用户存储”。 没有标志的默认值将放在用户存储中。

即使您从磁盘读取证书并将其存储在对象中,私钥仍存储在Microsoft Cryptographic API加密服务提供程序密钥数据库中。 在托管服务器上,ASP.NET进程无权访问用户存储。

另一种方法(根据下面的一些评论)是修改IIS配置或应用程序池标识 - 这确实有效。 但是,这假设可以访问这些配置项,这可能不是这种情况(例如,在共享托管环境中)。

我尝试过Randy将其更改为MachineKeySet的解决方案,但随后收到了错误消息:

“密钥无法在指定状态下使用”

所以经过一番谷歌搜索我发现一个帖子建议将其更改为:

var certificate = new X509Certificate2(certKeyFilePath, passCode, 
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet |       
X509KeyStorageFlags.PersistKeySet );

这解决了我的问题。

我还没有尝试过更改IIS配置中的设置应用程序池设置的建议。 要执行此操作,请转到站点应用程序池的“高级设置”,然后将“加载用户配置文件”设置为true。 如果此设置为false,则显然无法访问密钥容器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM