Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming WCF Service with BasicHttpBinding and Windows Authentication

Tags:

.net

wcf

I have a WCF service with the following binding:

<basicHttpBinding>        
  <binding name="bind" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
     <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message algorithmSuite="Default" clientCredentialType="UserName"/>
     </security>
  </binding>
</basicHttpBinding>

I am consuming it dynamically in code with this endpoint:

BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "bind";
binding.MaxBufferSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
ServiceEndpoint endPoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ImyContract)), binding, new EndpointAddress(endPointAddress));

endPoint.Name = name;
ChannelFactory<ImyContract> channelFactory = new ChannelFactory<ImyContract>(endPoint);

When I browse to the service everything works exactly as expected, however when connecting through the application I get:

Content Type text/xml; charset=utf-8 was not supported by service http://localhost/SynchronizationService/SyncService.svc/DataBinding. The client and service bindings may be mismatched.

I understand this error is caused often because basicHttpBinding using SOAP 1.1 and wsHttpBinding uses 1.2, however, I am not using SSL on the server, and have another endpoint using streaming, so switching is not an option for me. What am I doing wrong that is prevening me from consuming the service correctly?

UPDATE:

Also, I am not using the default basicHttpBinding on the client, but setting security to match what the server is expecting. I am setting the security with this method:

private static BasicHttpSecurity SetEndpointSecurity()
{
    BasicHttpSecurity security = new BasicHttpSecurity();
    HttpTransportSecurity transport = new HttpTransportSecurity();
    BasicHttpMessageSecurity message = new BasicHttpMessageSecurity();

    security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

    transport.ClientCredentialType = HttpClientCredentialType.Windows;
    transport.ProxyCredentialType = HttpProxyCredentialType.None;
    transport.Realm = "";

    message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
    message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;

    security.Transport = transport;
    security.Message = message;

    return security;
}
like image 880
Michael Kingsmill Avatar asked Nov 13 '22 14:11

Michael Kingsmill


1 Answers

Thanks the_ajp for your suggestion to let .NET build the proxy class and then re-implement its endpoint's in code as this led me to the correct solution!

As it turns out, my attempts to resolve the error "The client and service bindings may be mismatched.", led me to make the client and server endpoint bindings TOO similar. The key was to explicitly specify the TextEncoding as UTF-8 on the client endpoint and to utilize wsHttpBinding on the client, even though I was connecting to a basicHttpBinding on the server. This kept the message in SOAP 1.2 all the way from origin to destination, without requiring setting up SSL security on the server.

Thanks for all your help getting to the right solution guys!

Kris C, hopefully this can be of use to you in what you are working on next :)!

like image 118
Michael Kingsmill Avatar answered Nov 24 '22 01:11

Michael Kingsmill