4

I am trying to add a certificate to a web request to connect to Azure services.

My code looks like this:

string certThumbprint = "‎‎thumbprint";
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
     X509FindType.FindByThumbprint, certThumbprint, false);

Now I can confirm that the certificate does exist and the thumbprint is correct. However certCollection comes back empty.

Any ideas?

Update: here is how I open the cert store

certStore.Open(OpenFlags.ReadOnly);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
andrewb
  • 2,995
  • 7
  • 54
  • 95

3 Answers3

17

You probably have a hidden character or two at the very beginning of your thumbprint. I've made this mistake many times before when copying the thumbprint from the certificate manager in MMC. Here is a link for more information on this issue. http://support.microsoft.com/kb/2023835

Rick Rainey
  • 11,096
  • 4
  • 30
  • 48
2

A safe way to get the certificate thumbprints of the Personal Certificates store is to use an elevated instance of PowerShell.

PS C:\> dir cert:LocalMachine\My | select Thumbprint, FriendlyName, Subject

Peter P
  • 31
  • 1
0

I encountered the same issue today, while it's possible that there are hidden characters before and after the thumbprint it's also possible that if your debugging runs under a different user, the StoreLocation.CurrentUser isn't the same Store as the one you open in Windows.

I had this issue running in Service Fabric on localhost

Lonefish
  • 647
  • 2
  • 11
  • 32