C# 使用.Net标准2.0库中的x509证书使用WCF web服务?

C# 使用.Net标准2.0库中的x509证书使用WCF web服务?,c#,.net,wcf,authentication,ssl,C#,.net,Wcf,Authentication,Ssl,首先,感谢您的时间和关注 我有一个WCF web服务,需要x509证书进行身份验证。此web服务已添加为.Net标准2.0库中生成的连接服务和引用类。我想使用这个WCF web服务。然而,我遇到的是这个;我试图分配给BasicHttpsBinding的安全属性的BasicHttpsSecurity对象似乎没有任何可访问的构造函数 在这种情况下,如何将ClientCredentialType属性设置为Certificate 在我做的一个完整的.NETFramework测试项目中,我有与预期完全相同

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

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

在这种情况下,如何将ClientCredentialType属性设置为Certificate

在我做的一个完整的.NETFramework测试项目中,我有与预期完全相同的代码。绑定和x509证书似乎是根据需要设置的。我只是无法设置安全属性来指定我希望它实际使用证书…:/

下面是我用来设置客户端的代码。在完整的.Net Framework测试应用程序中,它似乎工作正常。主要的问题是“Security=new basichttpsecurity()”告诉我没有接受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标准2.0库中的x509证书的WCF web服务,我们非常感谢您提供的任何建议。如果我在这件事上做错了,请一定要引导我


提前感谢您的时间

嗯。。。今天早上终于弄明白了。而且,我对这个答案有点尴尬。但是,希望这能帮助其他可怜的人用头撞墙

事实证明,我可以在创建的绑定上设置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;
}

我真傻。。。在发布问题之前,我至少应该尝试一下,但我只是没有意识到这是可能的/

可以在配置文件@Suhail中启用SSL安全性,谢谢您的评论。我有一个appsettings.json配置文件,而不是传统的app/web.config。此WCF web服务将从ASPNet核心web API中调用。我计划从appsettings.json中提取应用程序设置,并在代码中填充绑定。有更好的办法吗?