422

c# Load PFX certificate from store or file

c# Load PFX certificate from store or file

To load a certificate from the windows certificate store you can easily try to find it by thumbprint like this

 
using System.Security.Cryptography.X509Certificates;

X509Store store = new X509Store( StoreLocation e.g. "LocalMachine", StoreName e.g. "My");

X509Certificate2Collection found = store.Certificates.Find( x509FindType, Thumbprint, validOnly);

if(found.Count == 1 ) 
   return found[0]; //this is your certificate
else 
   throw new Exception("Certificate load error");

To load a certificate from the file system

 
using System.Security.Cryptography.X509Certificates;

X509Certificate2Collection collection = new X509Certificate2Collection();

collection.Import(filePath, filePassword, X509KeyStorageFlags.PersistKeySet);

if(found.Count == 1 ) 
   return collection[0]; //this is your certificate
else 
   throw new Exception("Certificate load error");

Comments (0)


This thread has been closed from taking new comments.