Consuming a web service through an internet proxy server, using a WCF client in C#; providing proxy server authentication
Asked Answered
T

1

6

I have a client program that consumes a web service. It works quite well in a number of installations. Now I have a situation where a new customer connects to the internet via a proxy server, and my program's attempt to access the web service gets the "HTTP status 407: Proxy authentication required" error.

I thought that all the configuring of internet access, including proxy server address, port number and authentication would be done in the Control Panel Internet Options, and that I wouldn't have to worry about that in the code, or even in the app.config, of the Web Service client.

Have I got it all wrong?

What I have done in the mean time is give the user the chance to configure the proxy user name and password, and then in my code I do the following:

webServiceClient.ClientCredentials.UserName.UserName = configuredUsername;
webServiceClient.ClientCredentials.UserName.Password = configuredPassword;

But I don't know that this is the right thing. Because it seems to me that the above ClientCredentials would refer to the web service binding/security, not to the internet proxy server.

I suppose I can try it at the customer, but I'd rather be sure of what I'm doing first.

Tosha answered 4/11, 2009 at 8:55 Comment(0)
T
6

I found out how to do this thing, with the help of a contributor to another forum which in the flurry of trying all sorts of things I've forgotten. So thank you to that now forgotten person.

Here's the code that worked in the end (suitably disguised, but gives the right idea):

    BasicHttpBinding binding = new BasicHttpBinding("APISoap"); /* APISoap is the name of the binding element in the app.config */
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
    binding.UseDefaultWebProxy = false;
    binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyIpAddress, proxyPort)); 
    EndpointAddress endpoint = new EndpointAddress("http://www.examplewebservice/api.asmx");

    WebServiceClient client = new WebServiceClient(binding, endpoint);

    client.ClientCredentials.UserName.UserName = proxyUserName;
    client.ClientCredentials.UserName.Password = proxyPassword;
Tosha answered 6/11, 2009 at 12:26 Comment(4)
What if the web service requires its own credentials, do you know how to add them along with the proxy login?Luciano
This solution is working when the service and proxy need the same credentials, but in case both require credentials of their own then how to set the credentials. In case of NTLM authenticated proxy what changes need to be made?Photima
@Luciano and ajay: I'm sorry, I can't really answer that without doing a whole lot of research. I managed to get my solution working way back 5 years ago and never looked at this kind of thing again since then.Tosha
Perhaps the answer to two different authentications is to use something like a basic auth token in the header of your request instead of the user/pass directly?Pleiad

© 2022 - 2024 — McMap. All rights reserved.