加载证书以使用 WCF 在 C# 中签署 SOAP 信封

Load a certificate to sign a SOAP envelope in C# with WCF

我尝试加载 x509 证书以在 WCF 客户端中使用。为此,我使用 SetDefaultCertificate 函数,但此函数抛出异常。

var clientWS = new WS_eFacturaSoapPortClient();
clientWS.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
    StoreLocation.CurrentUser, StoreName.My, 
    X509FindType.FindBySubjectKeyIdentifier, "79852b4fab95e8cd1f6e36167bbb895bd4cbe767");

异常:

Cannot find the X.509 certificate using the following search criteria:
StoreName 'My', StoreLocation 'CurrentUser', FindType
'FindBySubjectKeyIdentifier', FindValue
'79852b4fab95e8cd1f6e36167bbb895bd4cbe767'.

但是如果我这样做...

X509Certificate2 cert = null;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
    store.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection col = store.Certificates.Find(
        X509FindType.FindBySubjectKeyIdentifier, "79852b4fab95e8cd1f6e36167bbb895bd4cbe767", true);
    cert = col[0];
}
//  Cerrar el store
finally { store.Close(); }

证书成立。

我做错了什么?可以将 x509Certificate2 添加到 ClientCredentials 吗?

我将 FindType 更改为 FindBySerialNumber 并且有效。

clientWS.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
    StoreLocation.CurrentUser, StoreName.My, 
    X509FindType.FindBySerialNumber, "0cf43655217b8853e2df0b931d2c352afa93d9");

this post帮了我。