2012-06-05 46 views
2

我有一個WCF服務和一個使用該服務的客戶端。他們使用WSHttpBinding和MTOM消息編碼。客戶端不使用app.config文件,並執行代碼中的所有端點配置。 它在正常的環境中都能很好地工作,但是現在有一些用戶試圖從http代理後面連接到服務。每當他們嘗試連接時,他們都會得到一個407(需要代理身份驗證)警告。通過http代理獲取WSHttpBinding

我設法建立了自己的測試環境,使用虛擬機和連接到代理的專用網絡,因此我可以模擬他們所看到的。 我已經嘗試在app.config中設置system.net useDefaultCredentials屬性,但這似乎沒有任何效果。我檢查了正在發送的數據包,並且它們不包含任何代理驗證標頭。通過代理查看網絡流量,他們確實使用該標題。

我也嘗試了將代理服務器硬編碼到客戶端,但這給出了「無法連接到服務器」異常。檢查數據包,客戶端發送3個小的代理,其中沒有一個是http請求,就是這樣。我不確定那裏有什麼。

我甚至爲了添加一個消息檢查器到客戶端,並手動將所需的頭插入到請求中,但是這並沒有顯示在頭中。

我在這裏碰到了我的繩索,我真的需要一個解決方案。關於我在這裏失蹤的任何想法或解決方案?

這(WCF Service with wsHttpBinding - Manipulating HTTP request headers)似乎很有希望,但我仍然堅持。編輯: 這是一部分代碼。

  var binding = new WSHttpBinding(); 
      binding.MaxReceivedMessageSize = 100000000; 
      binding.MessageEncoding = WSMessageEncoding.Mtom; 
      binding.ReaderQuotas.MaxArrayLength = 100000000; 
      binding.OpenTimeout = new TimeSpan(0, 2, 0); 
      binding.ReceiveTimeout = new TimeSpan(0, 2, 0); 
      binding.SendTimeout = new TimeSpan(0, 2, 0); 
      binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 

      binding.UseDefaultWebProxy = true; 
      // I've also tried setting this to false, and manually specifying the binding.ProxyAddress 

      binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; 
      var endpointAddress = new EndpointAddress(new Uri(hostUrl)); 
      clientProxy_ = new UpdaterServiceProxy(binding, endpointAddress); 

      // this behavior was the attempt to manually add the Proxy-Authentication header 
      //clientProxy_.Endpoint.Behaviors.Add(new MyEndpointBehavior()); 

      clientProxy_.ClientCredentials.UserName.UserName = userName; 
      clientProxy_.ClientCredentials.UserName.Password = password; 
      clientProxy_.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = 
        System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust; 
      // do stuff... 

回答

1

經過大量的實驗後,我取得了一些進展。事實證明,我可以手動指定代理,它會起作用。

WebProxy proxy = new WebProxy("http://x.x.x.x:3128", false); 
proxy.Credentials = new NetworkCredential("user", "pass"); 
WebRequest.DefaultWebProxy = proxy; 

該代碼在我實例化clientProxy_之前出現。以前我曾嘗試過這種方法,但由於沒有指定端口,所以它默默無聞。我仍然無法獲取在Windows Internet設置中指定的代理設置。我嘗試設置proxy.Credentials = CredentialCache.DefaultNetworkCredentials;,但這似乎也沒有效果。

我也遇到了一個問題,現在客戶端試圖使用ChainTrust驗證服務的證書,但鏈接證書的請求未使用代理設置。由於這是一個更具體的問題,我在這裏分別寫下:Certificate validation doesn't use proxy settings for chaintrust

所以我仍然希望獲得更多的幫助。

UPDATE: 我最終只是增加一個網絡配置對話框,我的應用程序,使用戶可以指定自己的代理服務器設置。我一直無法使自動代理設置正常工作。