Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a WCF service with Custom Binding from unmanaged C++

Tags:

c++

c#

wcf

c++-cli

I need to connect to a WCF service from a native C++ application. I tried the link below and it worked with wsHttpBinding.

Create WCF service for unmanaged C++ clients

However I need to connect to a WCF service with a custom binding. This is how the code for the Custom Binding looks like in my app config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="ResourceCenterEndpoint5">
          <mtomMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
            messageVersion="Default" maxBufferSize="65536" writeEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="8192"      maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </mtomMessageEncoding>
          <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                    maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Ntlm"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    keepAliveEnabled="true" maxBufferSize="65536"
                    proxyAuthenticationScheme="Anonymous"
                    realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                    useDefaultWebProxy="true" />
        </binding>
      </customBinding>
      </binding>
    </bindings>
    <client>
      <endpoint address="http://usaabcxyzas1.na.abc.com/Build15/ReserSVC/Resource.svc"
      binding="customBinding" bindingConfiguration="ResourceCenterEndpoint5"
      contract="ServiceReference2.ResourceCenterServiceContract"
      name="ResourceCenterEndpoint5">
        <identity>
          <userPrincipalName value="[email protected]" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

I have a bridge DLL which is a managed C++ DLL. The managed C++ DLL connects the C# Client to the native appliaction. However I am unable to connect to the Web Service from the managed C++ DLL as the web service is using custom binding. The error I get is:

The http request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the sever was 'Negotiate,NTLM'

This is how I tried to connect to the Webservice from the manged C++ dll:

Binding^ binding = gcnew BasicHttpBinding();

EndpointAddress^ address = gcnew EndpointAddress(gcnew String("http://usaabcxyzas1.na.abc.com/Build15/ReserSVC/Resource.svc"));

HelloServiceClient::ServiceReference2::ResourceCenterServiceContractClient^ client = gcnew HelloServiceClient::ServiceReference2::ResourceCenterServiceContractClient(binding, address); 
client->DoWork();

So basically I need to connect the managed C++ dll to the WCF Service with custom binding. How can I do this?

like image 316
abc123 Avatar asked Sep 12 '25 11:09

abc123


1 Answers

You are trying to use the BasicHttpBinding in your client code.

In the config file you require NTLM:

authenticationScheme="Ntlm"

The error points you to what you you have in the service's config file.

*The http request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the sever was 'Negotiate,NTLM'*

You also look like you've tried to hack in

proxyAuthenticationScheme="Anonymous"

So it comes down to your security requirements. If you want the service to not have security just take out the NTLM reference. If you want security you'll need a security section in your binding definition, something like:

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

Look at this article for more

like image 171
Dominic Tracey Avatar answered Sep 14 '25 01:09

Dominic Tracey