微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

问候,

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐