Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
Peter Avatar asked Nov 04 '09 08:11

Peter


People also ask

What is WCF client proxy?

Proxy is an object in memory on the client-side that exposes the same Interface or API that the WCF service does. Your consuming code will make calls against that proxy and proxy will dispatch those calls as SOAP Messages to the WCF service.

What is WCF service in C#?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.

How do I run a WCF web service?

Create and configure a console app project for hosting a WCF service. Add code to host the WCF service. Update the configuration file. Start the WCF service and verify it's running.

What is a WCF proxy class?

Actually Proxy is a class in WCF that is used to communicate with client application. We can simply get the entire configuration through the proxy class. There is no need to do extra effort to generate the configuration setting for the client. Proxy class used when you think that your service must be loosely coupled.


1 Answers

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;
like image 183
Peter Avatar answered Sep 22 '22 11:09

Peter