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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM