如何使用WCF服务临时停止证书错误

pet*_*ter 15 .net c# wcf

我正在测试我创建的WCF Web服务的早期版本.在客户端,当我使用VS来"添加服务引用"时,一切正常.

但是当我尝试使用该服务时,我得到错误,

Could not establish trust relationship for the SSL/TLS secure
channel with authority **
Run Code Online (Sandbox Code Playgroud)

星号代表服务器的IP地址.

无论如何在服务器上有一个安全证书,但它只是为了测试自己生成的,所以我暂时不担心证书错误.

在客户端,已经为我生成了app.config,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="BindingName" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="***************"
                binding="wsHttpBinding" bindingConfiguration="BindingName"
                contract="***************" name="BindingName">
                <identity>
                    <servicePrincipalName value="***************" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

那么我需要更改哪些设置才能暂时忽略证书错误?

Mic*_*ana 27

将CertificatePolicy PRIOR设置为在客户端上初始化WCF服务.这是如何(只需调用一次SetCertificatePolicy()方法)

 /// <summary>
    /// Sets the cert policy.
    /// </summary>
    private static void SetCertificatePolicy()
    {
        ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
    }

    /// <summary>
    /// Certificate validation callback 
    /// </summary>
    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
        if (error == SslPolicyErrors.None)
        {
           return true;   // already determined to be valid
        }

        switch (cert.GetCertHashString())
        {
           // thumbprints/hashes of allowed certificates (uppercase)
           case "066CF9CAD814DE2097D368F22D3A7D398B87C4D6":
           case "5B82C96685E3A20079B8CE7AFA32554D55DB9611":

              Debug.WriteLine("Trusting X509Certificate '" + cert.Subject + "'");
              return true;

           default:
              return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 14

<configuration>
  <system.net>
    <settings>
      <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
    </settings>
  </system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这适合我.谢谢


Ozr*_*ric 5

修改 web.config 对我有用

我使用 Steve Ellinger 的回答和一些谷歌搜索做到了。基本上,我必须:

  • 告诉 HTTP 连接管理器使用证书而不匹配证书名称和服务器主机名,并且不检查证书是否已被吊销
  • 修改客户端的端点行为以关闭证书验证

这是 web.config 片段...

<configuration>

  <system.net>
    <settings>
      <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
    </settings>
  </system.net>

  <system.serviceModel>
    <client>
      <endpoint ... behaviorConfiguration="DisableServiceCertificateValidation" />
    </client>

    <behaviors>
      <endpointBehaviors>
        <behavior name="DisableServiceCertificateValidation">
          <clientCredentials>
            <serviceCertificate>
              <authentication certificateValidationMode="None"
                              revocationMode="NoCheck" />
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>
Run Code Online (Sandbox Code Playgroud)