I'm building an application (win form) by C# .NET for document signing. I have got an error in signing. When I sign document on some computers (windows 7, windows 10, not in Windows Server), I got an error: "The keyset not defined".
So, Someone can tutorial or suggest me how can fix this problem? Thank so much!
This is my code:
// 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
...
I have solved this error. It is very easy. You select the "Platform target" to x86.
Right click on your project -> Properties -> Build -> Platform target -> x86
Regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With