在C#中连接到Web服务的问题

时间:2012-08-13 22:56:49

标签: c# wcf web-services wsdl

我正在使用第三方wsdl连接到服务。我已获得安全证书和用户名/密码。

我有:

  • 在我的Windows 7计算机上安装证书
  • 确保其拥有正确的权限
  • 拥有存储在web.config
  • 中的API的正确位置

代码每次都失败。错误消息会更改,但它们包括:

  • 身份验证失败,因为远程方已关闭传输流。
  • 远程主机强行关闭现有连接
  • 无法创建SSL / TLS安全通道

这是我正在执行的代码:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;

//Third party client
var client = new ConnectionPortClient();

//Including these two lines or not does not affect the outcome
//client.ClientCredentials.UserName.UserName = "username";
//client.ClientCredentials.UserName.Password = "password";

client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"C:\..\cert.p12", "password", X509KeyStorageFlags.MachineKeySet);

var results = client.getResults("");

这是web.config的相关部分:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="assessmentBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
            receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
            bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="Certificate" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://endpoint/" binding="basicHttpBinding"
          bindingConfiguration="assessmentBinding" contract="API.Assessment"
          name="assessmentSOAP" />
    </client>
  </system.serviceModel>

对这里发生了什么有任何想法?

1 个答案:

答案 0 :(得分:2)

您使用Certificate message credential type但是您正在尝试为UserName message credential type设置用户名/密码 - 这是错误的。查看有关Message Security with a Certificate Client

的文章