title: Pass Windows credentials to remote https WCF service
body: <p>I need some help, I'm trying to pass windows credentials to a WCF service. In the IIS only Windows authentication is enabled for those service and runs over https.</p>
  
  <p>The server side config is:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;protocolMapping&gt;
    &lt;add scheme="https" binding="basicHttpBinding" bindingConfiguration="httpsBinding"/&gt;
  &lt;/protocolMapping&gt;
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;binding name="httpsBinding"&gt;
        &lt;security mode="Transport"&gt;
          &lt;transport clientCredentialType="Windows"/&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior&gt;
        &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;          
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>and in the client side:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;binding name="BasicHttpBinding_IMyService" maxBufferPoolSize="2147483647"
        maxReceivedMessageSize="2147483647"&gt;
        &lt;security mode="Transport"&gt;
          &lt;transport clientCredentialType="Windows" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;client&gt;
    &lt;endpoint address="https://myserver.net:4343/MyService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
      contract="MyServiceReference.IMyService" name="BasicHttpBinding_IMyService" /&gt;
  &lt;/client&gt;
  </code></pre>
  
  <p></p>
  
  <p>I'm trying to consume the service on this way:</p>
  
  <pre><code>Client = new MyServiceClient();
  BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  binding.MaxReceivedMessageSize = int.MaxValue;
  binding.MaxBufferPoolSize = long.MaxValue;
  binding.MaxBufferSize = int.MaxValue;
  
  EndpointAddress ep = new EndpointAddress("https://myserver.net:4343/MyService.svc");
  Client = new COMINTSServiceClient(binding, ep);
  Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification;
  Client.ClientCredentials.Windows.ClientCredential =  System.Net.CredentialCache.DefaultNetworkCredentials;
  Client.Open();
  Array[] obj = Client.RandomMethod();
  </code></pre>
  
  <p>This code doesn't work for me:</p>
  
  <pre><code>    Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification;
      Client.ClientCredentials.Windows.ClientCredential =  System.Net.CredentialCache.DefaultNetworkCredentials;
  </code></pre>
  
  <p>In the service when ask for the user who is calling to the service using <code>ServiceSecurityContext.Current.WindowsIdentity.Name</code> allways get: <strong>ISS APPPOOL\ASP.NET v4.0</strong> instead of the domain\user who is calling</p>
  
  <hr>
  
  <p>The only way to make it work is write the username and password instead DefaultNetworkCredentials.</p>
  
  <pre><code>Client.ClientCredentials.Windows.ClientCredential.UserName = "DOMAIN\\user";
  Client.ClientCredentials.Windows.ClientCredential.Password = "passw";
  </code></pre>
  
  <p>But I don't want a user/passw hardcoded.
  Any help please?</p>