WCF Security error with VS 2008 Unit Test

.net, authentication, unit-testing, wcf

Solution

I had to change the following IIS and WCF service configurations to get past the "Negotiate,NTLM" exception.

IIS Configurations:

-- Unchecked "Anonymous Access" checkbox and check the "Integrated Windows authentication" checkbox in the directory security setting for the WCF Service virtual directory.

WCF Services:

-- implemented basicHttpBinding and configured the basicSettingBinding security setting to "TransportCredentialsOnly" mode and TransportClientCredentialType to "Windows"

Here is my updated wcf service configuration:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="windowsBasicHttpBinding">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
       </basicHttpBinding>
    </bindings>
    <services>
        <service    
      behaviorConfiguration="CityOfMesa.ApprovalRouting.WCFService.RoutingServiceBehavior"
           name="CityOfMesa.ApprovalRouting.WCFService.RoutingService"
        >
            <endpoint 
                binding="basicHttpBinding" bindingConfiguration="windowsBasicHttpBinding"
                name="basicEndPoint"    
                contract="CityOfMesa.ApprovalRouting.WCFService.IRoutingService" 
            />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior 
                name="CityOfMesa.ApprovalRouting.WCFService.RoutingServiceBehavior"
            >
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Problem

I was running my first Visual Studio 2008 Unit Test with a WCF Service and I received the following error: Test method UnitTest.ServiceUnitTest.TestMyService threw exception: System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.. I am also getting the following failed audit in the security log: Logon Failure: Reason: The user has not been granted the requested logon type at this machine User Name: (Internet Guest Account) Domain: Logon Type: 3 Logon Process: IIS Authentication Package: MICROSOFT_AUTHENTICATION_PACKAGE_V1_0 Workstation Name: I am hosting the WCF service in IIS 6.0 on a Windows XP SP3 machine. I have both the "Anonymous Access" and "Integrated Windows authentication" checked for the WCF service virtual directory. Here is my config file for the service: ``` <system.serviceModel> <services> <bindings> <basicHttpBinding> <binding name="MyBinding"> <security mode="None" /> </binding> </basicHttpBinding> <customBinding> <binding name="MyBinding"> <transactionFlow /> <textMessageEncoding /> <httpsTransport authenticationScheme="Ntlm"/> </binding> </customBinding> <wsHttpBinding> <binding name="MyBinding"> <security mode="None" /> </binding> </wsHttpBinding> </bindings> <service behaviorConfiguration="Service1Behavior" name="Service1" > <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyBinding" contract="IService1" > <identity> <dns value="localhost" /> </identity> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="Service1Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> ```

Original source