0

I'm trying to connect to WCF service via proxy. Proxy has authentication by login-password.

My code is:

var proxyHost = "http://127.0.0.1:3128/";
var proxyUserName = "username";
var proxyUserPassword = "userpassword";

var apiConnectAddress = "https://remoteaddress/service.svc";

var binding = new BasicHttpBinding
{
    UseDefaultWebProxy = false,
    ProxyAddress = new Uri(proxyHost)
    Security =
    {
        Mode = BasicHttpSecurityMode.Transport,
        Transport = 
        {
            ClientCredentialType = HttpClientCredentialType.None,
            ProxyCredentialType = HttpProxyCredentialType.Basic
        }
    }
};

var client = new Client(binding, new EndpointAddress(apiConnectAddress));

client.ClientCredentials.UserName.UserName = proxyUserName;
client.ClientCredentials.UserName.Password = proxyUserPassword;

client.SomeMethod(...); // an exception occurs here

class Client : System.ServiceModel.ClientBase<T> { ... }

Exception details:

System.ServiceModel.ProtocolException
    HResult=0x80131500
    Message=The remote server returned an unexpected response: (407) Proxy 
    Authentication Required.
    Source=System.Private.ServiceModel
    StackTrace:
        at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()

As I understand it, the exception occurs because login and password are not transmitted to the proxy server. How to configure HttpBinding to successfully connect to the service via proxy?

PS. The app written on .Net Core 2.2

Vlad
  • 1,377
  • 2
  • 17
  • 29

1 Answers1

1

According to the tips of the questioner, and as mentioned in the Github issue,
github.com/dotnet/wcf/issues/1592
There is no way to use proxy with authentication at present.
Thanks.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • I understand, that I don't need to provide client credentials, but I need to provide proxy server credentials. Also I already have generated WCF client that works correctly without using a proxy server. I don't see any using of proxy server in your examples. You directly connect to service with login and password. But I need to connect to service through authenticated proxy server. – Vlad Jul 02 '19 at 07:50
  • Sorry, maybe I have some deviations from the understanding of the issue. Please refer to the below link. https://stackoverflow.com/questions/17968307/how-to-set-proxy-credentials-to-specific-wcf-client/28206475 The binding configuration uses the Proxy address and UseDefaultWebProxy, ProxyCredentialType property to setup the credential, wish it is useful to you. – Abraham Qian Jul 02 '19 at 08:06
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/5f8d2c3a-164f-411a-b387-91f0be51f190/how-to-use-http-proxy-authentication-in-wcf?forum=wcf – Abraham Qian Jul 02 '19 at 08:29
  • it seems that useDefaultCredentials="true" is the key to the issue. – Abraham Qian Jul 02 '19 at 08:31
  • 1
    I saw this question. My code is exactly the same. `useDefaultCredentials` is not worked for me. – Vlad Jul 02 '19 at 09:53
  • According the GitHub issue, there is no way to use proxy with authentication. It's sad. Can you add this link and description to the answer? I'll accept the answer. – Vlad Jul 03 '19 at 09:08