Choose Certificate through CNPJ

0

I have this function that looks for the certificate by the serial:

 public static X509Certificate2 EscolherCertificado()
    {
        var store = new X509Store("MY", StoreLocation.CurrentUser);
        X509Certificate2 x509 = null;

        var Key = new RSACryptoServiceProvider();
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection collection = store.Certificates;
        X509Certificate2Collection fcollection = collection.Find(X509FindType.FindBySerialNumber, "serialcertificadoaqui", false);
        if (fcollection.Count == 1)
        {
            return fcollection[0];
        }
        else { return null; }

    }

But at first I will not know the serial, how can I get the certificate's serial number through a function? Without the user typing, is it possible?

    
asked by anonymous 29.11.2018 / 12:57

1 answer

0

I was able to solve this by changing this line:

X509Certificate2Collection fcollection = collection.Find(X509FindType.FindBySerialNumber, "serialcertificadoaqui", false);

for this:

 X509Certificate2Collection fcollection = collection.Find(X509FindType.FindBySubjectName, doc, false);

In doc I pass the cnpj only numbers, and it managed to find.

    
29.11.2018 / 14:05