Pass the certificate password v

1

Good afternoon, I am developing a system where it uses pfx certificate, as shown in the code below I pass the directory where it is, but I can not pass the password as a parameter, the main object of my system is to take the validity of the certificate .

public void ctf()
{

    // Caminho do certificado
    string Certificate = "C: CERTIFICADO.PFX";


    // Carregar o certificado em um objeto X509Certificate
    X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);

    // pega o valor( validade)
    string results = cert.GetExpirationDateString();

    // Exibe
    Console.WriteLine(results);

}
    
asked by anonymous 08.01.2018 / 20:33

1 answer

0

You should be able to get a collection object containing the certs in your .pfx file by using the X509Certificate2Collection class ... here's some C # example code:

string certPath = <YOUR PFX FILE PATH>;
string certPass = <YOUR PASSWORD>;

// Create a collection object and populate it using the PFX file
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

Then you can iterate over the collection:

foreach (X509Certificate2 cert in collection)
{
    Console.WriteLine("Subject is: '{0}'", cert.Subject);
    Console.WriteLine("Issuer is:  '{0}'", cert.Issuer);

    // Import the certificates into X509Store objects
}

Depending on the type of certificate (client cert, intermediate CA cert, root CA) you will need to open the proper cert store (as an X509Store object) to import it.

Check out the X509Store docs:

link

And the different members in the StoreName enumeration:

link

From what I understand, you want to use StoreName.My for client certificates that contain a private key, StoreName.CertificateAuthority for intermediate CA certs, and StoreName.Root for root CA certs.

    
23.12.2018 / 19:12