使用傳輸安全模式,證書建立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'
錯誤截圖:
【2】配置信息:
2.1服務端配置:
服務端設置證書,不采用客戶端安全認證。安全方式是傳輸安全。服務端配置信息如下:
- <services>
- <service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService" >
- <endpoint
- address="WCFService"
- binding="wsHttpBinding"
- bindingConfiguration="BasicWithTransport"
- contract="WCFService.IWCFService">
- </endpoint>
- <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
- <host>
- <baseAddresses>
- <add baseAddress="https://computer:9001/"/>
- </baseAddresses>
- </host>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="WCFService.WCFServiceBehavior">
- <serviceMetadata httpsGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="false" />
- <serviceCredentials>
- <serviceCertificate storeName="My" x509FindType="FindBySubjectName" findValue="WCFHTTPS" storeLocation="LocalMachine"/>
- </serviceCredentials>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <bindings>
- <wsHttpBinding>
- <binding name="BasicWithTransport">
- <security mode="Transport">
- <transport clientCredentialType="None"/>
- </security>
- </binding>
- </wsHttpBinding>
- </bindings>
2.2客戶端配置:
客戶端添加服務引用后,直接實例化類調用WCF服務,結果就出現不能為SSL建立信任關系錯誤。
- WCFClient.ClientProxy.WCFServiceClient wcfServiceProxyHttp = new WCFClient.ClientProxy.WCFServiceClient("WSHttpBinding_IWCFService");
- //通過代理調用SayHello服務
- string sName = "Frank Xu Lei WSHttpBinding";
- string sResult = string.Empty;
- 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服務之前,執行一次即可。代碼如下:
- public static class Util
- {
- /// <summary>
- /// Sets the cert policy.
- /// </summary>
- public static void SetCertificatePolicy()
- {
- ServicePointManager.ServerCertificateValidationCallback
- += RemoteCertificateValidate;
- }
- /// <summary>
- /// Remotes the certificate validate.
- /// </summary>
- private static bool RemoteCertificateValidate(
- object sender, X509Certificate cert,
- X509Chain chain, SslPolicyErrors error)
- {
- // trust any certificate!!!
- System.Console.WriteLine("Warning, trust any certificate");
- return true;
- }
- }
你要在調用操作點先調用這個方法: 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