I need to add a BinarySecurityToken to the WS security header of a SOAP request. I followed https://stackoverflow.com/a/22560639 and the Samples of MicrosoftWSE where FindCertificateByKeyIdentifier is used, however I would like to use the thumbprint so I made this function:
public static X509SecurityToken GetClientToken(string certThumbprint)
{
X509CertificateStore store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
if (store == null)
throw new ArgumentNullException("store");
X509SecurityToken token = null;
if (store.OpenRead())
{
Microsoft.Web.Services2.Security.X509.X509CertificateCollection certs =
store.FindCertificateByHash(Convert.FromBase64String(certThumbprint));
//store.FindCertificateByKeyIdentifier(Convert.FromBase64String(certThumbprint));
if (certs.Count > 0)
token = new X509SecurityToken(((Microsoft.Web.Services2.Security.X509.X509Certificate)certs[0]));
}
if (store != null)
store.Close();
return token;
}
However when I try to FindCertificateByHash I don't get the certificate.
In debugging mode I see that after store.OpenRead() the store has my certificate inside, yet under Non-Public members, m_thumbprint=null so I think the store doesn't load the certificates properly, why?
Note that in another scenario I use a System.Security.Cryptography.X509Certificates.X509Certificate2 which is retrieved by doing:
System.Security.Cryptography.X509Certificates.X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection =
store.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false);
This works, so I assume the certificate is installed correcly on my machine, still the certificate is not shown when using the X.509 Certificate tool WseCertificate2, maybe this is the reason why visual studio cannot load all the info? Due to this I was not able to get the key identifier to try to use FindCertificateByKeyIdentifier