Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问c中的x509certificate2时不存在密钥集#_C#_Cryptography - Fatal编程技术网

C# 访问c中的x509certificate2时不存在密钥集#

C# 访问c中的x509certificate2时不存在密钥集#,c#,cryptography,C#,Cryptography,当我在通过pfx安装x509Certificate2对象后第一次尝试使用该对象时,它可以对数据进行签名,但从第二次开始,它就无法进行签名,并显示一个错误,即该行中不存在密钥集 RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey; 在调试中还显示: A first chance exception of type 'System.Security.Cryptography.CryptographicE

当我在通过pfx安装x509Certificate2对象后第一次尝试使用该对象时,它可以对数据进行签名,但从第二次开始,它就无法进行签名,并显示一个错误,即该行中不存在密钥集

RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey;
在调试中还显示:

A first chance exception of type 
'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

我想不出这里的问题

我找到了解决办法

在第一次成功运行之后,私钥变得不可访问,因此产生了问题

解决方案是添加一行代码:

csp.PersistKeyInCsp = true;

它确保密钥可访问。

如果试图访问私钥的Windows用户没有访问私钥的权限,则可能会出现此错误。要修复打开的MMC并添加证书管理单元,请找到证书,右键单击“所有任务”->“管理私钥”,并授予该特定用户读取私钥的权限。

也许可以发布例外情况的更多信息。InnerException(如果有)…在它被破坏后,你做了什么使它再次工作,我也有同样的问题,在它第一次运行时它工作了,然后它失败了,即使我重新启动计算机它也失败了