22

I need to connect to some public wcf service, but there is some proxy between me and service. If i use default proxy settings such as

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

or

HttpWebRequest.DefaultWebProxy

it works perfectly fine but i don't need to set proxy settings for entire application, i need to set it for specific connection. So how I can do that?

I saw ProxyAddress property

(client.Endpoint.Binding as BasicHttpBinding).ProxyAddress

but there is no any properties for credentials... I was thinking to somehow modify HttpWebRequest, but I do not know how to get it...

Solved

Thank you all for your answers.

Answer of AntonK suitable for solving my problem.

At the time when this question was actual, I solved it in the same way, but without the use of web.config and wrote this method

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;
}
Pyfhon
  • 280
  • 1
  • 2
  • 9
  • 1
    Posting back the solution fantastic! Would that I could 5 you. – Stix Nov 02 '15 at 21:04
  • If the transport is HTTP not HTTPS then BasicHttpSecurityMode.Transport doesn't work but BasicHttpSecurityMode.TransportCredentialOnly does. However, for every web service call two requests are generated: the first one is sent without the Proxy-Authorization header and is rejected by the proxy with "407 Proxy Auth Required", and the second one contains the header and comes through. I wonder if there's a way to send the Proxy-Authorization header right away without waiting for a 407 response. – SlimShaggy Feb 20 '16 at 08:49
  • You should post an answer using your method. It's a fantastic answer. – Broots Waymb Jan 16 '18 at 18:46

3 Answers3

7

Here's an article dealing with this issue.

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

In summary, this is how to set a proxy for a specific service in the web.config. In the binding config, set proxyAddress="http://myproxy:8080" and set 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>
AntonK
  • 2,303
  • 1
  • 20
  • 26
0

I found a solution. You have to update WCF to the latest.

Go to NuGet Package Manager -> Update all related Project URL of WCF There must be:

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

This apply to .net core 2.1 version.

Warit Taveekarn
  • 339
  • 3
  • 7
-1

You can try this

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;

hope it helps

  • and where can I get request of wcf-service client instance? – Pyfhon Jul 31 '13 at 11:23
  • use wcf url while creting request HttpWebRequest request = HttpWebRequest.Create("http://localhost:8080/myservice/getdescription") as HttpWebRequest; – Nitor Mobile Team Jul 31 '13 at 12:18
  • Ok, a can find link in wsdl description and and it could work... So, do you propose to manually generate a Soap request and then parse response? – Pyfhon Jul 31 '13 at 12:59
  • This is almost identical to my needs. I have a working WCF service and it works through a proxy as well (by default). However, now I change my HTTP proxy to require username/password. I should be able to enter these programmatically. How can I set this starting from a reference to my service? MyServ s = new MyServ(); Don't see the answer here. – Jim Kennedy Mar 19 '14 at 19:37
  • Found this link, http://stackoverflow.com/questions/951523/how-can-i-set-an-http-proxy-webproxy-on-a-wcf-client-side-service-proxy. This post helped me understand which class I needs to reference. – Jim Kennedy Mar 20 '14 at 21:17