我正在尝试使用自签名证书(c#):
X509Certificate2 cert = new X509Certificate2(
Server.MapPath("~/App_Data/myhost.pfx"), "pass");
在一个共享虚拟主机服务器上,我得到了一个错误:
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
在我的dev机器上,它加载正常。之所以加载*.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版本
发布于 2009-08-28 06:48:09
将本地计算机存储用于私钥:
X509Certificate2 cert = new X509Certificate2("myhost.pfx", "pass",
X509KeyStorageFlags.MachineKeySet);
MachineKeySet
被描述为“私钥存储在本地计算机存储中,而不是当前的用户存储中”。没有标志的默认设置是放在用户存储中。
即使从磁盘读取证书并将其存储在对象中,私钥仍存储在Microsoft Cryptographic API Cryptographic Service Provider密钥数据库中。在宿主服务器上,ASP.NET进程没有访问用户存储的权限。
另一种方法(根据下面的一些注释)是修改IIS配置或应用程序池标识--这是可行的。然而,这假设可以访问这些配置项,但情况可能并非如此(例如,在共享主机环境中)。
发布于 2015-09-09 17:17:57
我尝试了Randy的解决方案,将其更改为MachineKeySet,但随后收到错误消息:
“密钥无效,无法在指定状态下使用”
所以在用谷歌搜索了一下之后,我发现了一个帖子,建议把它改成:
var certificate = new X509Certificate2(certKeyFilePath, passCode,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet );
这解决了我的问题。
我还没有尝试在IIS配置中更改设置应用程序池设置的建议。为此,请转到站点应用程序池的高级设置,然后将"load user profile“设置为true。当此设置为false时,密钥容器显然不可访问。
https://stackoverflow.com/questions/1345262
复制相似问题