【问题标题】:Consume WCF web service using x509 certificate in .Net Standard 2.0 Library?在 .Net Standard 2.0 库中使用 x509 证书使用 WCF Web 服务?
【发布时间】:2018-05-24 04:48:37
【问题描述】:

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

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

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

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

这是我用来设置客户端的代码。它似乎在完整的 .Net Framework 测试应用程序中运行良好。主要问题是“Security = new 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 服务的建议。如果我说这一切都错了,请直接引导我。

提前感谢您的宝贵时间!

【问题讨论】:

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

标签: c# .net wcf authentication ssl


【解决方案1】:

嗯……今天早上终于弄明白了。而且,我对答案有点尴尬。但是,希望这可以帮助其他一些可怜的人把他/她的头撞在墙上。

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

我真傻……我至少应该在发布问题之前尝试一下,但我只是没有点击我说这是可能的。 :/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 2018-11-17
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多