簡體   English   中英

如何為特定的 wcf 客戶端設置代理憑據?

[英]How to set proxy credentials to specific wcf client?

我需要連接到一些公共 wcf 服務,但是我和服務之間有一些代理。 如果我使用默認代理設置,例如

<system.net>
  <defaultProxy useDefaultCredentials="true" />
</system.net>

要么

HttpWebRequest.DefaultWebProxy

它工作得很好,但我不需要為整個應用程序設置代理設置,我需要為特定連接設置它。 那我該怎么做呢?

我看到了 ProxyAddress 屬性

(client.Endpoint.Binding as BasicHttpBinding).ProxyAddress

但是沒有任何憑據屬性......我想以某種方式修改HttpWebRequest,但我不知道如何獲得它......

解決了

謝謝大家的回答。

AntonK 的答案適合解決我的問題。

這個問題實際的時候,我也是用同樣的方法解決的,只是沒有使用web.config,寫了這個方法

void SetProxySettings<TChannel>(ClientBase<TChannel> client, 
    bool useProxy, string address, int port, string login, string password) 
    where TChannel : class
{
    if (!useProxy) return;
    var b = client.Endpoint.Binding as BasicHttpBinding;
    if (b == null)
    {
        System.Diagnostics.Debug.WriteLine("Binding of this endpoint is not BasicHttpBinding");
        return;
    }
    b.ProxyAddress = new Uri(string.Format("http://{0}:{1}", address, port));
    b.UseDefaultWebProxy = false; // !!!
    b.Security.Mode = BasicHttpSecurityMode.Transport;
    b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; // !!!
    b.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; // !!!
    if (client.ClientCredentials == null) return;
    client.ClientCredentials.UserName.UserName = login;
    client.ClientCredentials.UserName.Password = password;
}

這是一篇處理這個問題的文章。

http://blogs.msdn.com/b/stcheng/archive/2008/12/03/wcf-how-to-supply-dedicated-credentials-for-webproxy-authentication.aspx

總之,這就是如何在 web.config 中為特定服務設置代理。 在綁定配置中,設置proxyAddress="http://myproxy:8080"並設置useDefaultWebProxy="false"

<bindings>
  <basicHttpBinding>
     <binding name="SubscriberFulfilmentServiceSOAP12Binding" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="false"
proxyAddress="http://myproxy:8080"
      messageEncoding="Text">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

我找到了解決方案。 您必須將 WCF 更新到最新版本。

轉到 NuGet 包管理器 -> 更新 WCF 的所有相關項目 URL 必須有:

System.ServiceModel.Security
System.ServiceModel.NetTcp
System.ServiceModel.Http

這適用於 .net core 2.1 版本。

你可以試試這個

HttpWebRequest request = HttpWebRequest.Create("URI") as HttpWebRequest;
var proxy = new WebProxy(HttpWebRequest.GetSystemWebProxy().GetProxy(request.RequestUri), true);
proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword, DomainName);
request.Proxy = proxy;

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM