无法建立与基于 SOAP 的服务的连接

Unable to establish connection to a SOAP based service

我正在尝试设置对安全 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 request is not approved according to client schema Anonymous. Autentization header from server was Basic realm="CT".

当我检查 available bindings, I see that I'm supposed to use NetHttpsBinding (because we work against HTTPS as stated by the URL). The authentication method is suggested and I tried to roughly follow this approach.

这样的端点可以工作并生成正确的值,我使用 SoapUI 和基本身份验证以及同一组凭据来验证这些值。此外,据一般暗示,其他人 声称能够获取信息(尽管我怀疑那时有 Java 的客户)。

我尝试用谷歌搜索如何指定身份验证模式,但未能找到相关信息(或者我太无知以至于无法识别它的存在时间)。根据 this suggestion 我应该在请求中操纵 headers 。不过,我似乎无法在 FindUsersByUserIdRequest object 中访问该字段(覆盖它似乎是个坏主意,因为它是 auto-generated 而我无法控制它。

我该怎么做and/or我怎样才能进一步排除故障?

看起来(通过搜索此错误)您缺少一种指定“领域”的方法,您必须为请求执行此操作。您似乎可以在 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>

尽管不可否认这有点“有根据的猜测”,但请告诉我们这是否有效,如果无效,我可以指出一些可能相关的答案。