title: Calling third party secure webservice through WCF - authentication problems
body: <p>I'm writing a client against a vendor's webservice, using WCF in Visual Studio 2010.  I have no ability to change their implementation or configuration.</p>
  
  <p>Running against an install on their test server, I had no problems.  I added a service reference from their wsdl, set the url in code, and made the call:</p>
  
  <pre><code>var client = new TheirWebservicePortTypeClient();
  client.Endpoint.Address = new System.ServiceModel.EndpointAddress(webServiceUrl);
  
  if (webServiceUsername != "")
  {
      client.ClientCredentials.UserName.UserName = webServiceUsername;
      client.ClientCredentials.UserName.Password = webServicePassword;
  }
  
  TheirWebserviceResponse response = client.TheirOperation(myRequest);
  </code></pre>
  
  <p>Simple and straightforward.  Until they moved it to their production server and configured it to use https.  Then I got this error:</p>
  
  <pre><code>The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm='.
  </code></pre>
  
  <p>So I went looking for help.  I found this: <a href="https://stackoverflow.com/questions/1365163/can-not-call-web-service-with-basic-authentication-using-wcf">Can not call web service with basic authentication using wcf</a>.</p>
  
  <p>The approved answer suggested this:</p>
  
  <pre><code>BasicHttpBinding binding = new BasicHttpBinding();
  
  binding.SendTimeout = TimeSpan.FromSeconds(25);
  
  binding.Security.Mode = BasicHttpSecurityMode.Transport;
  binding.Security.Transport.ClientCredentialType = 
                                HttpClientCredentialType.Basic;
  
  EndpointAddress address = new EndpointAddress(your-url-here);
  
  ChannelFactory&lt;MyService&gt; factory = 
               new ChannelFactory&lt;MyService&gt;(binding, address);
  
  MyService proxy = factory.CreateChannel();
  
  proxy.ClientCredentials.UserName.UserName = "username";
  proxy.ClientCredentials.UserName.Password = "password";
  </code></pre>
  
  <p>Which also seemed simple enough.  Except for my trying to figure out which of the multitude of classes and interfaces that were generated from the wsdl to make the service reference I should use in place of the "MyService", above.</p>
  
  <p>My first try was to use "TheirWebservicePortTypeClient" - the class I had instantiated in the previous version.  That gave me a runtime error:</p>
  
  <pre><code>The type argument passed to the generic ChannelFactory class must be an interface type.
  </code></pre>
  
  <p>So I dug into the generated code, a bit more.  I saw this:</p>
  
  <pre><code>public partial class TheirWebservicePortTypeClient
  :
      System.ServiceModel.ClientBase&lt;TheirWebservicePortType&gt;, 
      TheirWebservicePortType
  {
      ...
  }
  </code></pre>
  
  <p>So I tried instantiating ChannelFactory&lt;> with TheirWebservicePortType.</p>
  
  <p>This gave me compile-time errors.  The resulting proxy didn't have a ClientCredentials member, or a TheirOperation() method.</p>
  
  <p>So I tried "System.ServiceModel.ClientBase".</p>
  
  <p>Instantiation ChannelFactory&lt;> with it still gave me compile-time errors.  The resulting proxy did have a ClientCredentials member, but it still didn't have a TheirOperation() method.</p>
  
  <p>So, what gives?  How do I pass a username/password to an HTTPS webservice, from a WCF client?</p>
  
  <p>==================== Edited to explain the solution ==================== </p>
  
  <p>First, as suggested, instantiation the factory with TheirWebservicePortType, adding the username and password to the factory.Credentials, instead of to proxy.ClientCredentials worked fine.  Except for one bit of confusion.</p>
  
  <p>Maybe it's something to do with the odd way the wsdl is written, but the client class, TheirWebservicePortTypeClient, defined TheirOperation as taking a Request argument and returning a Response result. The TheirWebservicePortType interface defined TheirOperation as taking a TheirOperation_Input argument and returning a TheirOperation_Output result, where TheirOperation_Input contained a Request member and TheirOperation_Output contained a Response member.</p>
  
  <p>In any case, if I constructed a TheirOperation_Input object from the passed Request, the call to the proxy succeeded, and I could then extract the contained Response object from the returned TheirOperation_Output object:</p>
  
  <pre><code>TheirOperation_Output output = client.TheirOperation(new TheirOperation_Input(request));
  TheirWebserviceResponse response = output.TheirWebserviceResponse;
  </code></pre>