How To Find a Certificate in the Certificate Store

21 באפריל 2013

tags: ,
no comments

I wrote a nice helper class that helps me find certificates installed on my machine.

Here is the code:

Code Snippet
  1. public static class CertificateHelper
  2.     {
  3.         public static X509Certificate2 FindCertificateByThumbprint(string certificateThumbprint)
  4.         {
  5.             var res = FindCertificateByThumbprint(certificateThumbprint, new X509Store(StoreName.My, StoreLocation.CurrentUser)) ??
  6.                       FindCertificateByThumbprint(certificateThumbprint, new X509Store(StoreName.My, StoreLocation.LocalMachine));
  7.  
  8.             if (res == null)
  9.                 throw new Exception(string.Format("No certificate found with the thumbprint {0} ", certificateThumbprint));
  10.  
  11.             return res;
  12.         }
  13.  
  14.  
  15.         public static X509Certificate2 FindCertificateByName(string subjectName)
  16.         {
  17.             var res = FindCertificateByName(subjectName, new X509Store(StoreName.My, StoreLocation.CurrentUser)) ??
  18.                       FindCertificateByName(subjectName, new X509Store(StoreName.My, StoreLocation.LocalMachine));
  19.  
  20.             if (res == null)
  21.                 throw new Exception(string.Format("No certificate found with the subjectName {0} ", subjectName));
  22.  
  23.             return res;
  24.         }
  25.  
  26.         private static X509Certificate2 FindCertificateByThumbprint(string certificateThumbprint, X509Store store)
  27.         {
  28.             X509Certificate2Collection certCollection = null;
  29.  
  30.             // Open the certificate store for the current user.
  31.             store.Open(OpenFlags.ReadOnly);
  32.  
  33.             // Find the certificate with the specified thumbprint.
  34.             certCollection = store.Certificates.Find(
  35.                 X509FindType.FindByThumbprint,
  36.                 certificateThumbprint,
  37.                 false);
  38.  
  39.             // Close the certificate store.
  40.             store.Close();
  41.  
  42.             // Check to see if a matching certificate was found.
  43.             if (0 == certCollection.Count)
  44.                 return null;
  45.  
  46.             // A matching certificate was found.
  47.             return certCollection[0];
  48.  
  49.         }
  50.         
  51.  
  52.         private static X509Certificate2 FindCertificateByName(string subjectName, X509Store store)
  53.         {
  54.             X509Certificate2Collection certCollection = null;
  55.  
  56.             // Open the certificate store for the current user.
  57.             store.Open(OpenFlags.ReadOnly);
  58.  
  59.             // Find the certificate with the specified thumbprint.
  60.             certCollection = store.Certificates.Find(
  61.                 X509FindType.FindBySubjectDistinguishedName,
  62.                 subjectName,
  63.                 false);
  64.  
  65.             // Close the certificate store.
  66.             store.Close();
  67.  
  68.             // Check to see if a matching certificate was found.
  69.             if (0 == certCollection.Count)
  70.                 return null;
  71.  
  72.             // A matching certificate was found.
  73.             return certCollection[0];
  74.  
  75.         }
  76.     }

Hope this helps

Manu

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*