在.Net Standard 2.0库中使用x509证书使用WCF Web服务?

时间:2018-05-24 04:48:38

标签: c# .net wcf authentication ssl

首先,感谢您的时间和关注!

我有一个WCF Web服务,需要x509证书才能进行身份验证。此Web服务已添加为.Net Standard 2.0库中生成的连接服务和引用类。我想使用此WCF Web服务。但是,我遇到的是这个;我尝试分配给BasicHttpsBinding的Security属性的BasicHttpsSecurity对象似乎没有任何可访问的构造函数......

如何在此方案中将ClientCredentialType属性设置为Certificate?

在我做的一个完整的.Net Framework测试项目中,我有完全相同的代码按预期工作。看来绑定和x509证书是根据需要设置的。我只是不能设置Security属性来指定我希望它实际使用证书...:/

这是我用来设置客户端的代码。它似乎在完整的.Net Framework测试应用程序中正常工作。主要关注的是"安全=新的BasicHttpsSecurity()"告诉我没有接受0参数的构造函数......但是,我根本没有看到任何构造函数。因此,我无法告诉绑定我想使用HttpClientCredentialType.Certificate作为我的ClientCredentialType。

private TestServiceClient GetTestServiceClient()
{
    var address = new EndpointAddress("https://known.to.be/working/service/endpoint");
    var binding = new BasicHttpsBinding()
    {
        Name = "basic_SSL_Cert",

        // ... other properties that work fine in full .Net Framework test.

        // I can't instantiate/initialize this...
        Security = new BasicHttpsSecurity()
        {
            Mode = BasicHttpsSecurityMode.Transport,
            Transport = new HttpTransportSecurity()
            {
                // ... so I can't set this :(
                ClientCredentialType = HttpClientCredentialType.Certificate,
                ProxyCredentialType = HttpProxyCredentialType.None
            }
        }
    };

    var client = new TestServiceCLient(binding, address);

    client.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine, 
        StoreName.My, 
        X509FindType.FindBySubjectDistinguishedName, 
        "I can find the cert no problem."
    );

    return client;
}

非常感谢有关如何使用需要来自.Net Standard 2.0库的x509证书的WCF Web服务的任何建议。如果我发现这一切都错了,请直接引导我。

提前感谢您的时间!

1 个答案:

答案 0 :(得分:0)

嗯......终于在今天早上弄清楚了。而且,我对这个答案感到有些尴尬。但是,希望这有助于其他一些可怜的灵魂将他/她的头撞在墙上。

事实证明,我可以在我创建的绑定上设置ClientCredentialType,而无需显式实例化和初始化BasicHttpsSecurity对象。

此绑定设置完美无缺......

private TestServiceClient GetTestServiceClient()
{
    var address = new EndpointAddress("https://known.to.be/working/service/endpoint");
    var binding = new BasicHttpsBinding()
    {
        Name = "basic_SSL_Cert",

        // ... other properties that work fine in full .Net Framework 

        //Security = new BasicHttpsSecurity()
        //{
        //    Mode = BasicHttpsSecurityMode.Transport,
        //    Transport = new HttpTransportSecurity()
        //    {
        //        // ... so I can't set this :(
        //        ClientCredentialType = HttpClientCredentialType.Certificate,
        //        ProxyCredentialType = HttpProxyCredentialType.None
        //    }
        //}
    };

    // Just set it... doh!
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

    var client = new TestServiceCLient(binding, address);

    client.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine, 
        StoreName.My, 
        X509FindType.FindBySubjectDistinguishedName, 
        "I can find the cert no problem."
    );

    return client;
}

我多么愚蠢......在发布问题之前,我至少应该尝试过这个问题,但我并没有点击跟我说这是可能的。 :/