简体   繁体   English

如何使用非托管C ++中的自定义绑定连接到WCF服务

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

I need to connect to a WCF service from a native C++ application. 我需要从本机C ++应用程序连接到WCF服务。 I tried the link below and it worked with wsHttpBinding . 我尝试了下面的链接,它与wsHttpBinding一起使用。

Create WCF service for unmanaged C++ clients 为非托管C ++客户端创建WCF服务

However I need to connect to a WCF service with a custom binding. 但是,我需要使用自定义绑定连接到WCF服务。 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="devlts_srv@na.abc.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

I have a bridge DLL which is a managed C++ DLL. 我有一个桥DLL,它是一个托管C ++ DLL。 The managed C++ DLL connects the C# Client to the native appliaction. 托管C ++ DLL将C#Client连接到本机应用程序。 However I am unable to connect to the Web Service from the managed C++ DLL as the web service is using custom binding. 但是,由于Web服务使用自定义绑定,因此无法从托管C ++ DLL连接到Web Service。 The error I get is: 我得到的错误是:

The http request is unauthorized with client authentication scheme 'Anonymous'. 使用客户端身份验证方案“匿名”未授权http请求。 The authentication header received from the sever was 'Negotiate,NTLM' 从服务器收到的身份验证标头是“Negotiate,NTLM”

This is how I tried to connect to the Webservice from the manged C++ dll: 这就是我尝试从manged C ++ dll连接到Webservice的方法:

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. 所以基本上我需要使用自定义绑定将托管C ++ dll连接到WCF服务。 How can I do this? 我怎样才能做到这一点?

You are trying to use the BasicHttpBinding in your client code. 您正尝试在客户端代码中使用BasicHttpBinding。

In the config file you require NTLM: 在配置文件中,您需要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. 如果您希望服务没有安全性,只需取出NTLM参考。 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 请看这篇文章了解更多

I don't think you want custom binding, as much as you want to customize the out of the box bindings. 我不认为你想要自定义绑定,因为你想要自定义开箱即用的绑定。 Unless your intention was to create a propietary communication protocol, outside the the TCP/IP etc. 除非您打算在TCP / IP之外创建一个专有的通信协议。

For the security issue, you would want to look into setting Security.Mode properties as well as assigning the right transport and/or message security properties. 对于安全性问题,您可能需要查看设置Security.Mode属性以及分配正确的传输和/或消息安全性属性。 eg. 例如。 use certificate or password challenge, encrypt, encrypt and sign etc. 使用证书或密码质询,加密,加密和签名等。

You'll also need to do the same on the client side. 您还需要在客户端执行相同的操作。 The binding should be almost identical to that of the server side. 绑定应该与服务器端的绑定几乎相同。

If you don't like basicHttp, there's always TCP, MSMQ, named piped and so on. 如果你不喜欢basicHttp,总会有TCP,MSMQ,名为管道等等。 You should look it up to get the full list. 你应该查找它以获得完整列表。

Have you tried to generate the WSDL from the web service using SvcUtil. 您是否尝试使用SvcUtil从Web服务生成WSDL。 Once you have the client proxy and the config which will have the configuration needed by client to connect to the service. 一旦你有了客户端代理和配置,它将具有客户端连接到服务所需的配置。

Also you did mention you want to connect using CustomBinding but in the client code you are using BasicHttpBinding. 您还提到要使用CustomBinding进行连接,但在使用BasicHttpBinding的客户端代码中。

One more thing you need your code it set is the AuthenticationScheme since the server is expecting scheme NTLM and your client code doesnt set that and by default it is Anonymous. 您需要设置代码的另一件事是AuthenticationScheme,因为服务器期望方案NTLM并且您的客户端代码没有设置它,并且默认情况下它是匿名的。

这看起来像是一个身份验证错误,所以你需要oto给自己更多的权限或找出谁得到它,但例如提供用户名和密码。

I maintain native c++ applications that consume Wcf services. 我维护使用Wcf服务的本机c ++应用程序。 Rather than deal with the raw connections and XML I will recommend using the excellent gSoap library. 我会建议使用优秀的gSoap库,而不是处理原始连接和XML。 This takes the WSDL from a service and generates code to operate with it. 这将从服务中获取WSDL并生成用于操作的代码。 By using the http://code.google.com/p/gsoapwininet/ plug-in all communication is directed via IE, which means that all the various windows authentication methods are automatically supported, which should solve your specific issue. 通过使用http://code.google.com/p/gsoapwininet/插件,所有通信都通过IE进行定向,这意味着将自动支持所有各种Windows身份验证方法,这将解决您的具体问题。

您可以创建customBinding并传入所需的绑定配置名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM