Search code examples
c#wcfweb-serviceswcf-bindingwcf-client

Using WCF to consume service and receieving varying errors depending on binding configuration


I am attempting to consume an intranet web service with WCF. I added reference to the service via the Add Service Reference feature in VS2008. In doing so I was prompted for network credentials to access the service which I provided and the service reference was added.

I then wrote some code that I would expect to fail as it doesn't pass credentials along with the call of the service:

FooServiceClient proxy = new FooServiceClient();
bool isValid = proxy.ValidateBar(baz);

When I use this code I receieve the exception:
The HTTP request is unauthorized with client authentication scheme 'Negotiate'.
The authentication header received from the server was 'Basic realm="Kerberos"'.

Which is the same error I receieve when using either of the two code examples below.

FooServiceClient proxy = new FooServiceClient();
proxy.ClientCredentials.UserName.UserName = "USERNAME";
proxy.ClientCredentials.UserName.Password = "PASSWORD";
bool isValid = proxy.ValidateBar(baz);

or

FooServiceClient proxy = new FooServiceClient();

NetworkCredential creds = new NetworkCredential("USERNAME", "PASSWORD");

proxy.ClientCredentials.Windows.AllowedImpersonationLevel =
  TokenImpersonationLevel.Identification;
proxy.ClientCredentials.Windows.AllowNtlm = false;
proxy.ClientCredentials.Windows.ClientCredential = creds;

bool isValid = proxy.ValidateBar(baz);

My gut tells me that I have the security mode configured incorrectly. According to the server manager the end point that I am attempting to bind to is looking for a Basic Http Credential via SSL. Which after reading about WCF-BasicHttp Transport Properties lead me to believe that I should use this configuration:

<security mode="Transport">
  <transport clientCredentialType="Windows" />
  <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

Unfortunately, I continued to receive the same error.

Again, I am sure my troubles have to do with a configuration issue on my part as I've previously consumed this service in other projects with the outdated Add Web Reference.


Solution

  • The below WCF binding configuration ended up being the solution.

    <security mode="Transport">
      <transport clientCredentialType="Basic" proxyCredentialType="None"
         realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>