首页 > 解决方案 > WCF 客户端。客户证书。第一次调用成功,第二次调用失败

问题描述

我正在尝试使用 c# .NET 4.5 使用soap11 和客户端证书凭据通过https 连接到soap Web 服务。我使用 vs 的工具来添加服务引用,它为我创建了一个肥皂客户端。

我创建了一个 customBinding 来通过 soap11 完成 https:

应用程序配置

<system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="*****" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:05:00" sendTimeout="00:05:00">
          <textMessageEncoding messageVersion="Soap11"/>
          <httpsTransport useDefaultWebProxy="false" requireClientCertificate="true" allowCookies="false" />
        </binding>
      </customBinding>
    </bindings>
    <client>
        <endpoint address="******************"
            behaviorConfiguration="******************"
            binding="customBinding" bindingConfiguration="******************"
            contract="******************"
            name="******************" />
    </client>
  <behaviors>
    <endpointBehaviors>
      <behavior name="******************">
        <clientCredentials>
          <clientCertificate findValue="******************" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
        </clientCredentials>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

c#代码:

     var client = new IConsultService.ConsultServiceClient();
     client.consult("1"); 
     client.consult("1"); <!-- fail error 404 - there was not endpoint listening at

如果我在没有证书的情况下发布接收 tomcat 404,如果我第一次发送客户端证书工作正常,在我看来我被重定向了。问题是在第二次请求失败(404)时,如果我再次实例化客户端失败。只有在停止应用程序并重新启动时才能再次工作。似乎有些东西被缓存在程序的静态堆栈中。

由于是 404,我的猜测是来自客户端证书,如果我在没有证书的情况下请求,则错误是相同的。

我尝试以编程方式创建绑定,设置 ClientBase.CacheSetting = CacheSetting.AlwaysOff 没有运气。

Edit1:使用 System.Net.HttpWebRequest 发生同样的情况,第一个请求正常,第二个请求 404。

Edit2:似乎握手第一个请求发送“证书,客户端密钥交换”和第二个请求“更改密码密钥交换”

有没有办法强制关闭通道并始终发送“证书,客户端密钥交换”?

在此处输入图像描述在此处输入图像描述

谢谢

标签: c#wcfnetwork-programmingssl-certificate

解决方案


首先,如果我们用X509certificate对客户端进行认证,我们一般需要建立服务端和客户端之间的信任关系,即我们应该在受信任的根证书颁发机构中安装彼此的证书,并且客户端需要提供一个Identity来确认服务器的身份(这通常是服务器证书的公钥,或主机名)

<endpoint address="net.tcp://10.157.13.69:4386/" binding="netTcpBinding"
    bindingConfiguration="NetTcpBinding_ITestService" contract="ServiceReference1.ITestService"
    name="NetTcpBinding_ITestService">
    <identity>
        <dns value="vabqia969VM" />
    </identity>
</endpoint>

其次,我怀疑这个问题是由 TLS 版本引起的。您是否尝试过升级这些项目的 Dotnetframework?至少4.6.2以上。请参考以下文件。
https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls
如果问题仍然存在,请随时告诉我。


推荐阅读