键集未定义

时间:2017-06-20 03:17:48

标签: c# .net windows digital-signature

我正在通过C#.NET构建一个应用程序(win form)来进行文档签名。我在签名时遇到错误。当我在某些计算机(Windows 7,Windows 10,而不是Windows Server)上签署文档时,出现错误:"未定义的键集"。         那么,有人可以教程或建议我如何解决这个问题?非常感谢!         这是我的代码:     // get certficate public X509Certificate2 LoadCertificateFromWindowsStore() { X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser); try { x509Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection numberCerts = (X509Certificate2Collection)x509Store.Certificates; X509Certificate2Enumerator certEnumerator; if (numberCerts.Count == 1) { certEnumerator = numberCerts.GetEnumerator(); while (certEnumerator.MoveNext()) return certEnumerator.Current; return null; } else if (numberCerts.Count > 1) { X509Certificate2Collection chooseCert = X509Certificate2UI.SelectFromCollection(numberCerts, "Certificates List", "Choose your certificate", X509SelectionFlag.SingleSelection);
if (chooseCert.Count == 1) return chooseCert[0]; else return null; } else return null; } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { x509Store.Close(); } return null; }
// using the cert to sign var cert = LoadCertificateFromWindowsStore(); if (cert.HasPrivateKey) // WORKS!!! {
signedXml.SigningKey = cert.PrivateKey; // THROW "keyset does not exist" EXCEPTION ...

1 个答案:

答案 0 :(得分:5)

我已经解决了这个错误。这很容易。您选择"平台目标"到x86。

右键点击您的项目 - >属性 - >构建 - >平台目标 - > 86

此致