Question

I am trying to connect to a WCF service but when I do I get this error:

The HTTP Request is unauthorized with client authetication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="ServiceGateway"'.

I know I do authentication as I set the client credentials on my WCF client:

new wcfClient(enpointconfigurationName, url) 
{
    ClientCredentials =
    {
        UserName =
        {
            UserName = "yyy", 
            Password = "zzz"
        }
    }
}

Edit

I have this WCF configuration in the web.config:

<client>
    <endpoint address="xxx" binding="basicHttpBinding" bindingconfiguration="myBinding" contract="yyy" name="myName" />
</client>

<basichttpbinding>
    <binding name="myBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
    </binding>
</basichttpbinding>
Was it helpful?

Solution

Config needs to be modified to use this in the binding:

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

EDIT

The reason it works is because that eventhough the UserName property is set in code the WCF service still needs to be configured to send the credentials. This is done in the B of the WCF ABC (http://msdn.microsoft.com/en-us/library/aa480190.aspx):

"A" stands for Address: Where is the service?
"B" stands for Binding: How do I talk to the service?
"C" stands for Contract: What can the service do for me?

The whole idea of WCF is that it should be configurable so that there is no need to redeploy the code if the service changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top