c# – 未定义键集

参见英文答案 > CryptographicException ‘Keyset does not exist’, but only through WCF                                    15个
我正在使用C#.NET构建一个应用程序(win form)来进行文档签名.我在签名时遇到错误.当我在某些计算机(Windows 7,Windows 10,而不是Windows Server)上签署文档时,出现错误:“未定义的密钥集”.
        那么,有人可以教程或建议我如何解决这个问题?非常感谢!
        这是我的代码
    //得到证书
    public X509Certificate2 LoadCertificateFromWindowsstore()
            {
                X509Store x509Store = new X509Store(StoreName.My,StoreLocation.CurrentUser);
                尝试
                {
                    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;
                    }
                    否则if(numberCerts.Count> 1)
                    {
                        X509Certificate2Collection chooseCert = X509Certificate2UI.SelectFromCollection(numberCerts,
                            “证书列表”,“选择您的证书”,X509SelectionFlag.SingleSelection);
                        if(chooseCert.Count == 1)
                            return chooseCert [0];
                        其他
                            return null;
                    }
                    其他
                        return null;
                }
                catch(CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                }
                最后
                {
                    x509Store.Close();
                }
                return null;
            }
    //使用证书签名
    var cert = LoadCertificateFromWindowsstore();
    if(cert.HasPrivateKey)//工作!!!
    {
        signedXml.SigningKey = cert.PrivateKey; // THROW“键集不存在”EXCEPTION
    …

解决方法:

我已经解决了这个错误.这很容易.您选择x86的“平台目标”.

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

问候,

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...