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

使用传输安全模式,证书建立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

WCFSecurityNegotiationException.gif(13.65 K)
8/11/2009 10:44:51 PM

【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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值