Say I selected a certificate from Windows' keystore, and at the time of signing, I need to make sure whether the SmartCard inserted in the reader is the right one or not...
Here is some sample code:
// finding the certificate
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
// by thumbprint, there is only one
certs = certs.Find(X509FindType.FindByThumbprint, "123456BLAHBLAHAE3C", true);
X509Certificate2 cert = certs[0];
RSACryptoServiceProvider key;
if (cert.HasPrivateKey)
{
// software cert
key = cert.PrivateKey as RSACryptoServiceProvider;
}
else
{
// certificate from smartcard
CspParameters csp = new CspParameters(1, "Microsoft Base Smart Card Crypto Provider");
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
key = new RSACryptoServiceProvider(csp);
}
Now when I want to sign data how can I know whether the smartcard that is currently inserted is actually the one I'm looking for (matching the one from Windows' keystore)?
I am asking this question, because right now when I sign the data, I get prompts for entering the PIN for the smartcard, but the card I inserted isn't even for the credential that I've selected... And it just signed the data anyway...