Keyset not defined
I am creating an application (win form) using C # .NET to sign a document. I have an error while signing. When I sign a document on some computers (Windows 7, Windows 10, not Windows Server), I received an error message: "Keyset not defined." So, can someone please advise or suggest me how to solve this problem? Many thanks! 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
...
source to share