System.Security.Cryptography.CryptographicException:在 Visual Studio 中抛出的密钥集不存在

Posted

技术标签:

【中文标题】System.Security.Cryptography.CryptographicException:在 Visual Studio 中抛出的密钥集不存在【英文标题】:System.Security.Cryptography.CryptographicException: Keyset does not exist thrown within Visual studio 【发布时间】:2014-09-14 17:22:23 【问题描述】:

美好的一天

由于此错误,我在尝试分配私钥时遇到严重问题。

System.Security.Cryptography.CryptographicException: Keyset does not exist

var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);

var col = store.Certificates.Find(X509FindType.FindBySerialNumber, "00873476DC47C5BB614EA96F2A9CE744A6", false);
var cert = col[0];
var xmlUnSignedSaml = new XmlDocument();
xmlUnSignedSaml.LoadXml(assertion);
xmlUnSignedSaml.PreserveWhitespace = true;
SignedXml signedXml = new SignedXml(xmlUnSignedSaml);

signedXml.SigningKey = cert.PrivateKey; //<<<--- Exception thrown.

我已验证以下内容:

    证书有一个私钥。 证书的读取权限授予 IUSR、网络服务、本地服务和 MMC 控制台上的本地用户上下文。证书位于 localMachine - Personal 文件夹中 对位于“C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys”的 machinekeys 文件夹授予相同的读取权限。

我已经检查了以下页面上的答案,但绝对没有一个对我有用:

Stack Overflow User @blowdart's answer MSDN Answer 其他网站,但这与设置 IIS 和获取证书以在那里工作有关

我在 Visual Studio 中运行应用程序,并且在上面的代码段中,它抛出了试图设置 SignedXml's SigningKey 的异常

我还能做些什么来启动和运行它? (事后我也尝试过证书和文件夹的“所有人”权限——即使抛出了同样的异常)

【问题讨论】:

【参考方案1】:

我已使用以下步骤解决了我的问题:

    从 Microsoft 下载 WCF_Samples 以访问 FindPrivateKey - 15.5mb in size 解决方案。 解压压缩文件 导航到 WCF_Samples\WCF\Setup\FindPrivateKey\CS\FindPrivateKey.sln 并构建它。ctrl+shift+B。 在Main(string args[])方法中添加如下代码行因为我比较懒,不想通过控制台做:args = new string[] "My", "LocalMachine", "-t", THUMBPRINT_OBTAINED_IN_MMC, "-a" ; 运行应用程序。我找到了钥匙的路径并导航到它。事实证明,密钥本身的权限未设置

我更改了密钥本身的权限,我的应用程序开始工作。

【讨论】:

以上是关于System.Security.Cryptography.CryptographicException:在 Visual Studio 中抛出的密钥集不存在的主要内容,如果未能解决你的问题,请参考以下文章