title: Xamarin : WCF Service and Integrated Windows Authentication
body: <p>I have an ASP.net application that hosts a WCF service and uses Integrated Windows Authentication, I am trying to consume this service in Xamarin Android application.</p>
  
  <p><strong>Application authentication settings in the IIS is configured as below.</strong>
  <img src="https://i.stack.imgur.com/Bvg95.png" alt="enter image description here"></p>
  
  <p><strong>Web.config at the service side defines service settings as below</strong></p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="HttpWindowsBinding"
                   maxReceivedMessageSize="2147483647"&gt;
            &lt;security mode="TransportCredentialOnly"&gt;
              &lt;transport clientCredentialType="Windows" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
        &lt;service name="EmployeeServicesWCF"&gt;
          &lt;endpoint address="http://localhost:801/CSE/EmpServices/EmployeeServicesWCF.svc"
                    binding="HttpWindowsBinding"
                    contract="CivilServicesEmpProj.IEmployeeServicesWCF" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p><strong>At the client side (Xamarin Android application) things arranged as follows</strong></p>
  
  <p>After generating proxy class using SvcUtil.exe and adding the class to the project, I use the following code to consume the service.</p>
  
  <pre><code>     BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
       binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  
       EndpointAddress endpoint = new EndpointAddress("http://10.0.2.2:801/CSE/EmpServices/EmployeeServicesWCF.svc"); 
  
       EmployeeServicesWCFClient client = new EmployeeServicesWCFClient(binding, endpoint);
       client.ClientCredentials.UserName.UserName = "10.0.2.2\\MyUser";
       client.ClientCredentials.UserName.Password = "mypass";
  
  
       Employee emp = client.GetEmployeeInfo();
  </code></pre>
  
  <p>When I run the code, I get this exception "System.NullReferenceException: Object reference not set to an instance of an object" at the only single line existing in the GetEmployeeInfo() method definition in the EmployeeDataServicesWCFClient class, please see below</p>
  
  <pre><code>public CSEDataContracts.Employee GetEmployeeInfo()
  {
      return base.Channel.GetEmployeeInfo(user);
  }
  </code></pre>
  
  <p>It worth mentioning that when I change authentication credentials in the above code to wrong credentials I get this exception instead "System.Net.WebException: There was an error on processing web request: Status code 401(Unauthorized): Unauthorized"</p>
  
  <p>Any idea what could be the problem?</p>