2012-05-24 49 views
0

構建客戶端應用程序以使用受到證書保護的WCF。 Cert已經安裝並且可以通過IE訪問WSDL,但是一旦它遇到CaseExists,應用程序就會拋出一個「FaultException`1被取消:無效證書」。有任何想法嗎?如果證書無效,那麼在IE中敲入WSDL時不會錯誤?FaultException`1未處理:無效的證書?

using System; 
using System.Configuration; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.ServiceModel.Security; 
using System.Security.Cryptography.X509Certificates; 
using System.ServiceModel.Description; 
using ConsoleApplication1.ServiceReference1; 
using System.Diagnostics; 
using System.ServiceModel.Channels; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // binding 
      BasicHttpBinding b = new BasicHttpBinding(); 
      b.Security.Mode = BasicHttpSecurityMode.Transport; 
      b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 

      // endpoint 
      EndpointAddress ea = new EndpointAddress(
       "https://cut out endpoint.svc"); 

      // fire it up 
      EBondingClient client = new EBondingClient(b, ea); 

      // toss in cert    
      client.ClientCredentials.Peer.PeerAuthentication.CertificateValidationMode = 
       System.ServiceModel.Security.X509CertificateValidationMode.None; 

      client.ClientCredentials.ClientCertificate.SetCertificate(
       StoreLocation.CurrentUser, 
       StoreName.My, 
       X509FindType.FindBySubjectName, 
       "cut.out.cert.name"); 

      // call 
      Console.WriteLine(client.CaseExists("hello world")); 
      client.Close(); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

WSDL不適用與實際服務相同的安全性。這樣消費者就可以訪問服務的模式,而不必知道如何進行身份驗證。 – mellamokb

+0

在這個前提下,我會同意你的意見;但是構建這個wsdl/wcf的人也擁有保護wsdl的證書。這是我第一次處理wcf,並且在這之前只通過javascript在類中處理傳統的soap/wsdl的東西。我的第一反應是「爲什麼你需要安裝的證書才能訪問wsdl?」 - 有可能證書對於訪問WSDL而不是端點有效嗎? –

回答

1

您正在得到一個輸入錯誤異常(這就是FaultException``1的含義)。據我所知,這些只能由服務器代碼明確拋出。如果服務主機檢測到證書問題,則應拋出MessageException

我會檢查CaseExists的實際代碼,看它是否拋出,並從那裏開始。此外,請嘗試捕獲FaultException並查看Detail屬性的對象類型,因爲通常包含有關故障的更多信息。 (具體而言,每個單獨FaultException<T>具體類型將有一個public T Detail屬性。)

另外,我不認爲相關,但你確定你需要指定客戶端證書?這與保護服務(和WSDL)的服務器端證書是分開的。對於服務器來說,要求客戶端提供證書是很不尋常的(雖然並非前所未有),所以我會確認你做的是正確的事情。

+0

謝謝!當我開始工作時,我會看看這個明天。我會告訴你。 –