X509 Cannot find the requested object

I have a certificate.cer file in the same directory (copy, if new) with the RSA key inside, but when I try:

string certificateFile = Environment.CurrentDirectory + "\\Certificate.cer"; X509Certificate2 x509 = new X509Certificate2(X509Certificate.CreateFromCertFile(certificateFile)); 

I get the same

"Cannot find the requested object"

mistake. How can I not get the error?

+8
c # x509certificate x509
source share
2 answers

You can simply pass the file name to the new () method.

Try:

 X509Certificate2 x509 = new X509Certificate2(certificateFile); 

If the certificate has a password, you must also specify it (where password is a string):

 X509Certificate2 x509 = new X509Certificate2(certificateFile, password); 
+3
source share
  • Show what your variable certificateFile contains.
  • Better to use Path.Combine to concatenate parts of the path, rather than adding them and inserting the slash manually.
0
source share

All Articles