【问题标题】:Acessing a WebService (HTTPS) that requires a certificate via WCF in C#在 C# 中通过 WCF 访问需要证书的 Web 服务 (HTTPS)
【发布时间】:2014-12-12 17:40:17
【问题描述】:

我是客户端计算机,我尝试访问的服务器是一个程序,我将为其提供支持(我是一个新的第 3 方程序),然后我需要发送一些数据。

他们使用 HTTPS,但我无法连接:

public void StartProcess() 
{
    BasicHttpsBinding binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
    // - They said they don't require credentials

    EndpointAddress address = new EndpointAddress("https://www.outsideservice.com/services/C001");

    OutsideServiceClient client = new OutsideServiceClient(binding, address);

    DataToSend data = new DataToSend();
    // - Filling up whatever data I need to send, omitted

    try
    {
        client.StartProcess(data);
    }
    catch (Exception ex)
    {
        // ex = Could not establish trust relationship for SSL/TLS secure channel
    }
}

他们已经有其他第 3 方程序访问他们的服务器。 我对证书的工作原理不是很了解(除了MSDN 上的基本阅读)。

在询问我如何访问他们的服务器后,我得到了答复: “已经通过 https 访问我们的服务器,只需访问机器,下载证书并在您的客户端机器上使用。”

在谷歌搜索了我是如何做到这一点之后,我尝试从here 访问https://theirURL.com/certsrv,但我收到Resource Not Found 错误。

根据我对证书如何工作的理解,我必须从他们那里获得证书,双击它,安装在我的机器(或运行代码以连接到它们的机器)上能连接对吗? 或者我需要做些什么才能连接?

【问题讨论】:

    标签: c# .net web-services wcf ssl


    【解决方案1】:

    “我必须从他们那里获得证书,双击它,安装在我的机器上(或运行代码连接到他们的机器上)才能连接对吗?”

    这是做事的正确方法,但是要启动并运行,您可以使用以下代码:

    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    

    我通常喜欢将其包装在不将此应用于发布版本的代码中,以便生产实际上必须正确安装证书,如下所示:

    #if (!Release)
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    #endif
    

    我相信您应该可以将它放在您喜欢的任何地方,例如 Global.asax.cs 或在调用之前。

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 2011-02-11
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2011-01-15
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多