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

Eon*_*Eon 5 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.
Run Code Online (Sandbox Code Playgroud)

我已经验证了以下内容:

  1. 证书具有私钥.
  2. 读取证书的权限授予IUSR,NETWORK SERVICE,LOCAL SERVICE和MMC控制台上的本地用户上下文.证书位于localMachine - Personal文件夹中
  3. 对"C:\ Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys"中的machinekeys文件夹赋予相同的读取权限.

我已经在以下几页检查了答案,但绝对没有一个对我有效:

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

我还能做些什么才能让它运行起来?(事后补充,我也尝试过对cert和文件夹的"Everyone"权限 - 即使抛出相同的异常)

Eon*_*Eon 4

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

  1. 从 Microsoft 下载了 WCF_Samples 以访问 FindPrivateKey - 大小为 15.5mb 的解决方案。
  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. 运行应用程序。我找到了密钥的路径并导航到它。事实证明,密钥本身的权限未设置

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