I have to put a digital certificate of type CPF A3 token with password in an HttpWebRequest, so I was trying to use the X509Certificate2, as follows:
private X509Certificate2 GetCert(string CertFile, string CertPass)
{
FileStream fs = new FileStream(CertFile, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
X509Certificate2 cert = new X509Certificate2(buffer, CertPass);
fs.Close();
fs.Dispose();
return cert;
}
//chamo assim
GetCert("C:\certificado.cer", password);
But I was informed that this was to read certificates on my computer, so I tried the code below
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
//my.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
my.Open(OpenFlags.ReadOnly);
// Find the certificate we'll use to sign
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
var x509 = cert;
byte[] rawData = x509.RawData;
Console.WriteLine("Content Type: {0}", X509Certificate2.GetCertContentType(rawData));
Console.WriteLine("Serial Number: {0}", x509.SerialNumber);
Console.WriteLine("Friendly Name: {0}", x509.FriendlyName);
//continue
Just that this code is working OK only for CNPJ certificates, when I use CPF it asks me to pry a certified pen drive ... One more strange thing is: he is reading all the certificates that one day I already I installed it on my computer ...