views:

5756

answers:

4

I am in my rookie season with WCF Services...

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>
+1  A: 

The default authentication is windows (or NTLM) so you'll need to specify that you don't want authentication in your config file.

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="myBinding">
        <security mode="None" />
      </binding>
  </bindings>
</system.serviceModel>

also add this attribute to the endpoint

bindingConfiguration="myBinding"

The binding element specifies modifications of the standard behavior of the wsHttpBinding.

Then the "bindingConfiguration="myBinding" attribute on the endpoint says that that endpoint should use the modifications we specified.

Karg
Did not work. I received the same error message.
Michael Kniskern
Updated to security mode None
Karg
Still does not work....I even added the bindingName attribute to the service node.
Michael Kniskern
k, tried with message security on and credentials none.Also lowered my certainty that the solution will work
Karg
Also added the bindingConfiguration attribute to add to the endpoint element
Karg
When I tried to update the service reference, I got the following message: "The servrice certificate is not provided. Specify a service certificate in ServiceCredentials"
Michael Kniskern
I removed the bindingConfiguration attribute and I did not received that error from the previous comment
Michael Kniskern
k, try setting the security back to none and include the bindingConfiguration in the endpoint element
Karg
should I set bingingConfiguration to "MyBinding"?
Michael Kniskern
yes, (well, to "myBinding"). That can be changed to something more appropriate as long as it matches in both places
Karg
I am still getting the same error. I have updated the source code to match your input
Michael Kniskern
also made it a community wiki
Michael Kniskern
I gave you an up vote for helping trouble shooting this issue....:)
Michael Kniskern
+1  A: 

When you have securityMode="None" in your binding, you should turn off integrated authentication.

jezell
I tried that and received the following error when I tried to update the service reference in the test project: Metadata contains a reference that cannot be resolved ''. The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authenication header received from the ser
Michael Kniskern
Cont....server was ''. The remote server returned an error: (401) Unauthorized.
Michael Kniskern
Can you browse to the WSDL? If you disable integrated authentication and enable anonymous authentication and have SecurityMode="None" on both the client and the server, it should work.
jezell
yes, I can browse to the WSDL in IE 7.
Michael Kniskern
Check out the local security policy on the machine and make sure that anonymous users aren't locked out from network access. Most likely, the issue is in the AD group policy or local security policy for the machine.
jezell
I also ran this service in a web application and console application and received the same "The remote server returned an error: (401) Unauthorized" for both tests.
Michael Kniskern
I gave you an up vote for helping trouble shoot this issue....:)
Michael Kniskern
+2  A: 

After finally pulling out my hair over this issue....(wait I have no hair!)

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>
Michael Kniskern
A: 

As a side note.....There was a GPO setting "NTLM Authentication Level" that was controls authenication that was causing the unit test to generate the "Negotiate,NTLM" exception.

Michael Kniskern