title: Connect to a password protected server with WCF over HTTP
body: <p><strong>Hi,</strong></p>
  
  <p>I need to connect my ASP.NET MVC application to a server on the net with WCF. The server is username and password protected but it appears that its not a https but a simple http.</p>
  
  <p>This is the code I have so far : </p>
  
  <pre><code>public MyObject GetMyData(string id)
  {
      ChannelFactory&lt;MyIService&gt; factory;
      ClientCredentials loginCredentials = new ClientCredentials();
  
      loginCredentials.UserName.UserName = "MyName";
      loginCredentials.UserName.Password = "MyPassword";
  
      factory = new ChannelFactory&lt;ICFService&gt;("MyData");
  
      var defaultCredentials = factory.Endpoint.Behaviors.Find&lt;ClientCredentials&gt;();
      factory.Endpoint.Behaviors.Remove(defaultCredentials); //remove default ones
      factory.Endpoint.Behaviors.Add(loginCredentials); //add required ones
  
      var proxy = factory.CreateChannel();
      var response = proxy.GetMyData(id);
      ((IDisposable)proxy).Dispose();
  
      return response as MyObject ;
  }
  </code></pre>
  
  <p>And this is placed in my web.config</p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;client&gt;
        &lt;endpoint address="http://MyUrl/"
                  binding="webHttpBinding"
                  behaviorConfiguration="MyData"
                  contract="MyNameSpace.MyIService"
                  name="MyData"/&gt;
      &lt;/client&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="MyData"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
  
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The problem is that I get the following exception :</p>
  
  <p><em>The remote server returned an error: (401) Unauthorized.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.</em></p>
  
  <p>I know that the server that I am trying to communicate with will only communicate if my application is placed on a specific IP nr but this is already done and should work.</p>
  
  <p><strong>Pleas advice</strong></p>
  
  <p><strong>Edit1:</strong> </p>
  
  <p>This is how the service looks like : </p>
  
  <pre><code>[ServiceContract]
  [XmlSerializerFormat]
  public interface ICFService
  {
      [OperationContract]
      [WebGet(BodyStyle = WebMessageBodyStyle.Bare, 
          ResponseFormat = WebMessageFormat.Xml, 
          UriTemplate = "get?UID=12345.eu_vddsall_xml&amp;MyId={MyId}&amp;LANG=en")]
      CFExtendedDescription GetMyData(string MyId);
  
  }
  </code></pre>
  
  <p><strong>Edit2:</strong> 
  This is how the dialog looks like when browsing the url in firefox. Im not sure how to check the acctual url that the Proxy.GetMyData will use.</p>
  
  <p><img src="https://i.stack.imgur.com/K8Scq.png" alt="AccessDenied">
  <em>Pleas note, this is an example, the adress and "The site says" is something else.</em></p>
  
  <p>The Url that I am supose to use looks like this : </p>
  
  <pre><code>http://bat.myserver.com/ag/get?UID=12345.eu_vddsall_xml&amp;MyId=WRH421&amp;LANG=sv
  </code></pre>
  
  <p><strong>Edit3:</strong>
  By adding the foolowing to the web.config we got a step forward (thanks Ladislav Mrnka) :</p>
  
  <pre><code>&lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="secured"&gt;
        &lt;security mode="TransportCredentialOnly"&gt;
          &lt;transport clientCredentialType="Basic" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  </code></pre>
  
  <p>Now we get the following exception instead : </p>
  
  <p><em>Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService',  '<a href="http://tempuri.org/" rel="nofollow noreferrer">http://tempuri.org/</a>')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  Exception Details: System.Runtime.Serialization.SerializationException: Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService',  '<a href="http://tempuri.org/" rel="nofollow noreferrer">http://tempuri.org/</a>')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.</em></p>
  
  <p>First I thought that the remote server maby returned a xml document that shows that I am on the wrong IP so I uploaded the site to the correct server(with the correct ip) but I get the same exception there?</p>
  
  <p><strong>Edit 4</strong></p>
  
  <p>Created a new question : <a href="https://stackoverflow.com/questions/7248617/unable-to-deserialize-xml-body-with-root-name-binary-and-root-namespace">Unable to deserialize XML body with root name &#39;Binary&#39; and root namespace?</a></p>
  
  <p>Thanks Ladislav Mrnka for helping!</p>