C#:从x509certificateStore加载证书时找不到CrifificateByhash

发布于 2025-01-22 21:57:14 字数 1824 浏览 1 评论 0原文

我需要向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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2025-01-29 21:57:14

您可以尝试这样的事情。

public static X509Certificate2 TryGetCertificate(string thumbprint, StoreLocation location = StoreLocation.LocalMachine)
{
    using var store = new X509Store(StoreName.My, location);
    store.Open(OpenFlags.ReadOnly);
    thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();

    X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
    if (certificateCollection.Count == 0)
    {
        return null;
    }

    return certificateCollection[0];
}

You can try something like this.

public static X509Certificate2 TryGetCertificate(string thumbprint, StoreLocation location = StoreLocation.LocalMachine)
{
    using var store = new X509Store(StoreName.My, location);
    store.Open(OpenFlags.ReadOnly);
    thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();

    X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
    if (certificateCollection.Count == 0)
    {
        return null;
    }

    return certificateCollection[0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文