Avkash Chauhan's Blog

Windows Azure, Windows 8, Cloud Computing, Big Data and Hadoop: All together at one place.. One problem, One solution at One time...

What to do when your code could not find the certificate in Azure VM?

What to do when your code could not find the certificate in Azure VM?

Rate This
  • Comments 1

In an Windows Azure application when both client and service are running in two different Windows Azure virtual machines, I found some strange behavior.

 

I found that the following code which was using client certificate to authenticate with WCF service was keep failing:

certStore.Certificates.Find(X509FindType.FindByThumbprint, Certificate_ThumbPrint, true);

 

Above code fails to find the certificate even though certificate is available which I verify in the Azure VM under Certificate MMC.

 

I was not able to get a definitive answer why Find Certificate is not working in Azure VM, due to unsupported scenario or something else. To expedite the solution I used the following approach in my Code:

  1. Open Certificate Store
  2. Loop through all the certificate in specific Certificate storage and match each thumbprint to find the one you are looking for.

 

The code snippet look like as below:

 public static X509Certificate2 GetExpectedCertificate(CertStoreName certStoreName, StoreLocation certStoreLocation, string certThumbprint)
{
X509Store store = new X509Store(certStoreName.ToString(), certStoreLocation);
try
{
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = new X509Certificate2Collection();
foreach (X509Certificate2 cert in store.Certificates)
{
if(cert.Thumbprint.Equals(certThumbprint))
certCollection.Add(cert);
}
if (certCollection.Count == 0)
{
throw new ArgumentException(string.Format("Unable to find the certificate – Certificate Store Location ={0} Certificate Store Name={1} Certificate Thumbprint={2}", certStoreLocation, certStoreName, certThumbprint));
}
}
finally
{
store.Close();
}
}


Leave a Comment
  • Please add 1 and 2 and type the answer here:
  • Post
  • your work is good and it is full of knowledge thanks for the share.

Page 1 of 1 (1 items)