• WCF分布式开发常见错误(24):Could not establish trust relationship for the SSL/TLS secure channel with authority(转)


    使用传输安全模式,证书建立SSL,宿主端口证书配置完毕,但是客户调用服务出错。
    【1】错误信息:
    Could not establish trust relationship for the SSL/TLS secure channel with authority 'computer:9001'.
    不能和授权计算机为 SSL/TLS 安全通道建立信任关系.
    WCF中文论坛问题连接:不能和授权计算机为 SSL/TLS 安全通道建立信任关系:Could not establish trust relationship for the SSL/TLS secure channel with authority 'computer:9001'
    错误截图:
    WCF分布式开发常见错误(24):Could not establish trust relationship for the SSL/TLS secure channel with authority_20041

    【2】配置信息:
      2.1服务端配置:
      服务端设置证书,不采用客户端安全认证。安全方式是传输安全。服务端配置信息如下:

    1. <services>
    2.       <service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService" >
    3.         <endpoint
    4.           address="WCFService"
    5.           binding="wsHttpBinding"
    6.           bindingConfiguration="BasicWithTransport"
    7.           contract="WCFService.IWCFService">
    8.         </endpoint>
    9.         <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
    10.         <host>
    11.           <baseAddresses>
    12.             <add baseAddress="https://computer:9001/"/>
    13.           </baseAddresses>
    14.         </host>
    15.       </service>
    16.     </services>
    17.     <behaviors>
    18.       <serviceBehaviors>
    19.         <behavior name="WCFService.WCFServiceBehavior">
    20.           <serviceMetadata httpsGetEnabled="true" />
    21.           <serviceDebug includeExceptionDetailInFaults="false" />
    22.           <serviceCredentials>
    23.               <serviceCertificate  storeName="My"  x509FindType="FindBySubjectName" findValue="WCFHTTPS" storeLocation="LocalMachine"/>
    24.           </serviceCredentials>
    25.         </behavior>
    26.       </serviceBehaviors>
    27.     </behaviors>
    28.     <bindings>
    29.     <wsHttpBinding>
    30.       <binding name="BasicWithTransport">
    31.         <security mode="Transport">
    32.           <transport clientCredentialType="None"/>
    33.         </security>
    34.       </binding>
    35.     </wsHttpBinding>
    36.     </bindings>
    复制代码

    2.2客户端配置:
        客户端添加服务引用后,直接实例化类调用WCF服务,结果就出现不能为SSL建立信任关系错误。

    1.                 WCFClient.ClientProxy.WCFServiceClient wcfServiceProxyHttp = new WCFClient.ClientProxy.WCFServiceClient("WSHttpBinding_IWCFService");
    2.                 //通过代理调用SayHello服务
    3.                   string sName = "Frank Xu Lei WSHttpBinding";
    4.                 string sResult = string.Empty;
    5.                 sResult = wcfServiceProxyHttp.SayHello(sName);
    复制代码

    【3】问题分析:
          Could not establish trust relationship for the SSL/TLS secure channel with authority 'computer:9001'.
    不能和授权计算机为 SSL/TLS 安全通道建立信任关系.
          实际原因和证书有很大关系,这里证书是跟证书颁发机构信任的证书,在客户端和服务端建立安全会话的时候,无法信任此证书。
        另外一个可能的原因是你其他域里也使用此一个证,这个也有可能导致错误。
    【4】解决办法:
        3.1:定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一个类,然后在客户单调用WCF服务之前,执行一次即可。代码如下:

    1. public static class Util
    2.     {
    3.         /// <summary>
    4.         /// Sets the cert policy.
    5.         /// </summary>
    6.         public static void SetCertificatePolicy()
    7.         {
    8.             ServicePointManager.ServerCertificateValidationCallback
    9.                       += RemoteCertificateValidate;
    10.         }
    11.         /// <summary>
    12.         /// Remotes the certificate validate.
    13.         /// </summary>
    14.         private static bool RemoteCertificateValidate(
    15.           object sender, X509Certificate cert,
    16.             X509Chain chain, SslPolicyErrors error)
    17.         {
    18.             // trust any certificate!!!
    19.             System.Console.WriteLine("Warning, trust any certificate");
    20.             return true;
    21.         }
    22.     }
    复制代码

    你要在调用操作点先调用这个方法: Util.SetCertificatePolicy();                 sResult = wcfServiceProxyHttp.SayHello(sName);
        3.2:就是需要你在客户端和服务端各安装一个跟证书授权机构。然后制作一受信任的根证书机构的证书。可以参考这个:
    http://www.codeplex.com/WCFSecur ... ringTitle=How%20Tos
        【5】总结:
          对Windows Server服务器产品开发部署WCF服务的时候才采用的第二种机制。需要授权的证书机构颁发的证书。对于普通的学习第一种方式就可以了。     WCF安全开发编程实践,是一个比较复杂的过程,除了需要掌握基本的安全知识以外,要需要熟练运用各种证书制作,安装、SSL证书httpcfg.配置等工具。在Windows Server2003,Vitsa系统下差别还很大,普通的XP系统下开发学习更是需要安装一写服务,而且调试过程也比较繁琐,一旦有点配置不对,就会出现异常。需要耐心去学习。  
    参考资料: 1.Could not establish trust relationship for the SSL/TLS secure channel with authority PC1 2.WCF Could not establish trust relationship for the SSL/TLS secure channel with authority

  • 相关阅读:
    linux下如何解压和压缩文件
    windows如何查看jdk的安装目录
    java.sql.SQLException: Data truncated for column 'seat_number' at row 1
    Error:(23, 0) Could not find method implementation() for arguments [directory 'libs'] on object of t
    grandle Project sync failed.please fix your project and try again
    java如何实现批量删除pdf指定的页数
    Java实现批量将word文档转换成PDF
    Linux下查看目录文件数和文件大小
    IDEA启动Springboot时,解决报错java.lang.NoClassDefFoundError: javax/servlet/Filter
    如何SpringBoot项目改为外置Tomcat启动
  • 原文地址:https://www.cnblogs.com/quietwalk/p/2891689.html
Copyright © 2020-2023  润新知