微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在C#中建立与基于SOAP的服务的连接

如何解决无法在C#中建立与基于SOAP的服务的连接

我正在尝试建立对安全HTTPS端点的调用,并使用以下代码建立基于SOAP的通信。我使用Visual Studio和服务的WSDL创建了Web服务参考。

//NetHttpsBinding binding = new NetHttpsBinding();
NetHttpsBinding binding = new NetHttpsBinding();

string url = "https://" + ...;
EndpointAddress address = new EndpointAddress(url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//CustomClient client = new CustomClient();
CustomClient client = new CustomClient(binding,address);
client.ClientCredentials.UserName.UserName = "monkey";
client.ClientCredentials.UserName.Password = "donkey";

FindUsersByUserIdRequest request = new FindUsersByUserIdRequest
{
  applicationId = "kaweb",searchMatchMode = "EXACT",soughtUserId = "197002150485"
};

FindUsersByUserIdResponse response = client.FindUsersByUserId(request);
User[] result = response.users;

计算机不喜欢它,并告诉我类似的信息,并指定了401。 (某事,因为它使用的是我们不懂的怪异语言。是的,在地狱中有一个特殊的角落,专门用于那些不会使用英语的人,因为它是哦,天哪,非常容易理解。)

System.ServiceModel.Security.MessageSecurityException:
根据客户端架构匿名,Http请求未获批准。服务器的增强化标头是Basic realm =“ CT”。

当我检查available bindings时,我发现我应该使用NetHttpsBinding(因为我们按照URL所述对HTTPS进行了处理)。建议使用here身份验证方法,我尝试大致遵循this approach

这样的端点可以正常工作并产生正确的值,我使用SoapUI和具有相同凭据集的基本身份验证对此进行了验证。此外,根据一般提示其他人声称能够获取信息(尽管我怀疑当时有基于Java的客户端)。

我尝试谷歌搜索如何指定身份验证模式,但是未能找到相关信息(或者太无知以致无法识别何时存在)。根据{{​​3}},我应该在请求中处理标头。我似乎并没有在FindUsersByUserIdRequest对象中访问该字段(并且覆盖它似乎是一个坏主意,因为它是自动生成的,我无法控制它。

我该怎么做和/或如何进一步对其进行故障排除?

解决方法

看起来(从搜索此错误中发现)您缺少一种指定对请求必须执行的“领域”的方式。您似乎可以在app-config.xml

中进行设置

eg1:

<basicHttpBinding>
   ...  
     <security mode="TransportCredentialOnly">
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
        <transport clientCredentialType="Ntlm" proxyCredentialType ="Ntlm" realm="CT"/>
      </security>
    </binding>
   </basicHttpBinding>
   ...

eg2:

 <security mode="Transport">
   <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="CT">
      <extendedProtectionPolicy policyEnforcement="Never" />
   </transport>
   <message clientCredentialType="UserName" algorithmSuite="Default" />
 </security>

eg3:

<security mode="Transport">
   <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="CT" />
</security>

尽管可以肯定地说是“有根据的猜测”,但让我们知道这是否可行,因为我可以指出一些可能相关的答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。