2
I have to put a digital certificate 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
Only that this code is working OK only for CNPJ certificates, when I use CPF it asks me to stick a pen drive with certificate... One more strange thing is: it is reading all the certificates that I once installed on the computer...
I believe that the certificate exported from a Smart Card or a USB stick does not have private key. The right one would read from the same USB.
– Leonel Sanches da Silva
I’m a little lost between private key and password... I know I need to not only stick the thumb drive, put a password to use it on Chrome for example...
– Andre
Diego, he gets all the same certificates, we have several of them installed, you can check them through on IE. In the case of the CPF he should ask for the USB drive because there is the certificate, it is not?
– Éderson C. do Nascimento
In fact, I understood this better, now I’m just having problems in how to put the password in the certificate.... What is the order to do? How do I add the password to the certificate?
– Andre
I didn’t test the code, but this guy says he was able to add the PIN automatically. https://helpdev.com.br/2017/08/11/comor-addir-pinsenha-automaticamente-no-certificado-a3/
– diegodsp