C#:从x509certificateStore加载证书时找不到CrifificateByhash
我需要向SOAP请求的WS安全标头添加一个二进制安全性。我关注 https://stackoverflow.com/a/a/22560639 和Microsoftwse的样品以及FindCertificateByKeyendefier的样品使用拇指打印机,因此我制作了此功能:
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;
}
但是,当我尝试找到CertificateByhash时,我没有获得证书。
在调试模式下,我看到sore.openread()商店内有我的证书,但是在非公共成员的情况下,m_thumbprint = null,所以我认为商店没有正确加载证书,为什么?
请注意,在另一种情况下,我使用system.security.cryptography.x509certificates.x509certificate2,它可以通过以下操作检索
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);
以下操作,因此我假设证书已在我的机器上纠正,但使用X.509时仍未显示证书证书工具wsecertificate2,也许这就是Visual Studio无法加载所有信息的原因?因此,我无法获得关键标识符来尝试使用FindCertificateByKeyIdentifier
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这样的事情。
You can try something like this.