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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With