c# - 如何使用非托管 C++ 中的自定义绑定(bind)连接到 WCF 服务

标签 c# c++ wcf c++-cli

我需要从 native C++ 应用程序连接到 WCF 服务。我尝试了下面的链接,它与 wsHttpBinding 一起使用。

Create WCF service for unmanaged C++ clients

但是,我需要使用自定义绑定(bind)连接到 WCF 服务。这就是自定义绑定(bind)代码在我的应用配置文件中的样子

<?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>

我有一个桥接 DLL,它是一个托管 C++ DLL。托管 C++ DLL 将 C# 客户端连接到 native 应用程序。但是,我无法从托管 C++ DLL 连接到 Web 服务,因为 Web 服务正在使用自定义绑定(bind)。我得到的错误是:

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

这就是我尝试从管理的 C++ dll 连接到 Web 服务的方式:

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();

所以基本上我需要使用自定义绑定(bind)将托管 C++ dll 连接到 WCF 服务。我该怎么做?

最佳答案

您正在尝试在客户端代码中使用 BasicHttpBinding。

在您需要 NTLM 的配置文件中:

authenticationScheme="Ntlm"

错误将您指向服务配置文件中的内容。

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

你看起来也试图入侵

proxyAuthenticationScheme="Anonymous"

因此,这取决于您的安全要求。如果您希望该服务没有安全性,只需取出 NTLM 引用。如果您想要安全,您需要在绑定(bind)定义中添加一个安全部分,例如:

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

this article for more

关于c# - 如何使用非托管 C++ 中的自定义绑定(bind)连接到 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722347/

相关文章:

c# - 空 json 列表被视为 null

c# - 添加对 ASP.NET Web API 服务的服务引用

c# - 具有特殊字符转换的字符串

c# - 如何在java中编写可为空的int?

c++ - 在 C++ 中模板化友元

c++ - 如果我可以旋转我的角色,我该如何使用网格贴图进行碰撞检测?

c++ - std::bit_cast 与 std::array

c# - 通过 ProxyServer 发送 WCF 请求

c# - WCF 上的 X.509 证书?

c# - .Net-通过Outlook发送的电子邮件应更新数据库