System.Security.Cryptography.CryptographicException:Visual Studio中不存在键集

时间:2014-07-23 13:35:42

标签: c# cryptography certificate private-key

美好的一天

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

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.

我已经验证了以下内容:

  1. 证书有私钥。
  2. 读取证书的权限授予IUSR,NETWORK SERVICE,LOCAL SERVICE和MMC控制台上的本地用户上下文。证书位于localMachine - 个人文件夹
  3. 对“C:\ Documents and Settings \ All Users \ Application Data \ Microsoft \ Crypto \ RSA \ MachineKeys”中的machinekeys文件夹赋予相同的读取权限。
  4. 我已经在以下页面检查了答案,但绝对没有一个对我有效:

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

    我还能做些什么才能让它正常运行? (事后补上,我也尝试过&#34; Everyone&#34;对证书和文件夹的许可 - 即使是同样的例外)

1 个答案:

答案 0 :(得分:4)

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

  1. 从Microsoft下载WCF_Samples以获取对该帐户的访问权限 FindPrivateKey - 15.5mb in size解决方案。
  2. 提取zip文件
  3. 导航到WCF_Samples \ WCF \ Setup \ FindPrivateKey \ CS \ FindPrivateKey.sln并构建它。ctrl+shift+B
  4. Main(string args[])方法中添加了以下代码行,因为我很懒,不想通过控制台执行此操作:args = new string[] { "My", "LocalMachine", "-t", THUMBPRINT_OBTAINED_IN_MMC, "-a" };
  5. 跑完应用程序。我找到了钥匙的路径并导航到它。事实证明,密钥本身的权限未设置
  6. 我更改了密钥本身的权限,我的应用程序开始工作。

相关问题