title: WCF client caching windows authentication
body: <p>I am calling a webservice exposed by Navision, and it is secured with windows authentication. I am able to call it successfully, but after that, it seems like it is caching the credentials somehow, and that is what worries me.</p>
  
  <p>The service is hosted on a remote server, and is in a different domain than my development machine. I am running the code from Visual Studio.</p>
  
  <p>I have created a service reference to the service, and I have no configuration in my app.config, so all settings are created using code.</p>
  
  <p>First run (no client credentials specified):</p>
  
  <pre><code>var binding = new BasicHttpBinding();
  binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  
  var address = new EndpointAddress("http://externalserver.com/DynamicsNAV/WS/Customer/Page/MyPage");
  
  var client = new MyPage_PortClient(binding, address);
  client.ClientCredentials.Windows.AllowNtlm = true;
  client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
  
  var reqObj = new MyPage() { TypeID = "Test", Company_Name = "Test:" + DateTime.Now.ToShortTimeString() };
  client.Create(ref reqObj);
  client.Close();
  Console.WriteLine(reqObj.Company_Name);
  Console.ReadLine();
  </code></pre>
  
  <p>This gives me a securityexception. As Expected.</p>
  
  <p>Second run (with credentials):</p>
  
  <pre><code>var binding = new BasicHttpBinding();
  binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  
  var address = new EndpointAddress("http://externalserver.com/DynamicsNAV/WS/Customer/Page/MyPage");
  
  var client = new MyPage_PortClient(binding, address);
  client.ClientCredentials.Windows.ClientCredential.Domain = "MYDOM";
  client.ClientCredentials.Windows.ClientCredential.UserName = "NavWebService";
  client.ClientCredentials.Windows.ClientCredential.Password = "foo";
  client.ClientCredentials.Windows.AllowNtlm = true;
  client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
  
  var reqObj = new MyPage() { TypeID = "Test", Company_Name = "Test:" + DateTime.Now.ToShortTimeString() };
  client.Create(ref reqObj);
  client.Close();
  Console.WriteLine(reqObj.Company_Name);
  Console.ReadLine();
  </code></pre>
  
  <p>This call succeeds. Again, as expected.</p>
  
  <p>Third run, is the same as the first one. That is, with no credentials specified. That call succeeds. Now I'm confused. The credentials must somehow be cached? I reboot my machine, same result. Still succeeds.</p>
  
  <p>I then try and specify bogus credentials:</p>
  
  <pre><code>var binding = new BasicHttpBinding();
  binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  
  var address = new EndpointAddress("http://externalserver.com/DynamicsNAV/WS/Customer/Page/MyPage");
  
  var client = new MyPage_PortClient(binding, address);
  client.ClientCredentials.Windows.ClientCredential.Domain = "fakeMYDOM";
  client.ClientCredentials.Windows.ClientCredential.UserName = "fakeNavWebService";
  client.ClientCredentials.Windows.ClientCredential.Password = "badPwd";
  client.ClientCredentials.Windows.AllowNtlm = true;
  client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
  
  var reqObj = new MyPage() { TypeID = "Test", Company_Name = "Test:" + DateTime.Now.ToShortTimeString() };
  client.Create(ref reqObj);
  client.Close();
  Console.WriteLine(reqObj.Company_Name);
  Console.ReadLine();
  </code></pre>
  
  <p>This call fails. As expected.</p>
  
  <p>I go back to the first call again, and that still succeeds. So it is actually still caching the credentials from the first succeeded call, even though I have tried with invalid credentials in the meantime.</p>
  
  <p>Can anyone tell me what is going on here? Is it something that I don't understand about windows authentication? Is there some sort of credential caching in Visual Studio/WCF?</p>