天天看點

WCF分布式開發常見錯誤(24):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 安全通道建立信任關系.

錯誤截圖:

【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'.

       實際原因和證書有很大關系,這裡證書是跟證書頒發機構信任的證書,在用戶端和服務端建立安全會話的時候,無法信任此證書。

    另外一個可能的原因是你其他域裡也使用此一個證,這個也有可能導緻錯誤。

【4】解決辦法:

    3.1:定義一個類,來對遠端X.509證書的驗證,進行處理,傳回為true.我們要自己定義一個類,然後在客戶單調用WCF服務之前,執行一次即可。代碼如下:

 public static class Util

    {

        /// <summary>

        /// Sets the cert policy.

        /// </summary>

        public static void SetCertificatePolicy()

        {

            ServicePointManager.ServerCertificateValidationCallback

                       += RemoteCertificateValidate;

        }

        /// Remotes the certificate validate.

        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:就是需要你在用戶端和服務端各安裝一個跟證書授權機構。然後制作一受信任的根證書機構的證書。可以參考這個:

<a href="http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How">http://www.codeplex.com/WCFSecurity/Wiki/View.aspx?title=How%20To%20-%20Create%20and%20Install%20Temporary%20Certificates%20in%20WCF%20for%20Message%20Security%20During%20Development&amp;referringTitle=How%20Tos</a>

    【5】總結:

      對Windows Server伺服器産品開發部署WCF服務的時候才采用的第二種機制。需要授權的證書機構頒發的證書。對于普通的學習第一種方式就可以了。

     WCF安全開發程式設計實踐,是一個比較複雜的過程,除了需要掌握基本的安全知識以外,要需要熟練運用各種證書制作,安裝、SSL證書httpcfg.配置等工具。在Windows Server2003,Vitsa系統下差别還很大,普通的XP系統下開發學習更是需要安裝一寫服務,而且調試過程也比較繁瑣,一旦有點配置不對,就會出現異常。需要耐心去學習。

參考資料:

 本文轉自 frankxulei 51CTO部落格,原文連結:http://blog.51cto.com/frankxulei/320957,如需轉載請自行聯系原作者

繼續閱讀