Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Find Certificate by thumbprint via AppSetting

This one is really weird and I can't figure out why one method works and one doesn't.

I have a certificate in my local computer store and a thumbprint for it. The app uses the cert when making HTTP web requests so I need to fetch it. I want to store the thumbprint in the web.config as an AppSetting value. Whenever I pull the AppSetting value and use it to Find the certificate, it doesn't find it. However, if I make a local variable (class variable, readonly, const, whatever...) and search by it, it works. I've done String.Compare() on both the value is exactly the same. What gives? I tried to look at the IL to see if I can see something funky but nothing.

' This Works '
Dim certificateThumbprint As String = "D0650C9D31CF525D3C82153DCEBC3C3265D75FE3"
Dim certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, False)

' This doesn't '
Dim appSettingcertificateThumbprint = System.Web.Configuration.WebConfigurationManager.AppSettings("CertificateThumbprint")
Dim certCollection2 = certStore.Certificates.Find(X509FindType.FindByThumbprint, appSettingcertificateThumbprint, False)

' Intermediate window shows that '
String.Compare(certificateThumbprint, appSettingcertificateThumbprint, True) = 0
like image 502
dhodge7 Avatar asked Sep 26 '12 20:09

dhodge7


1 Answers

So it turns out when you copy and paste the thumbprint string from the certificate's properties window, it's possible to also carry over an invisible hex character that won't show up in your Visual Studio window. (see here)

What I ended up doing was copying the thumbprint to Notepad++, going a line below it, typing it exactly how I saw it, copying it back to the web.config, and running from there. That seemed to work out perfectly.

like image 136
antinescience Avatar answered Oct 04 '22 22:10

antinescience