【问题标题】:WCF - 'The service certificate is not provided for target' error for WCF client calling a WCF serviceWCF - 调用 WCF 服务的 WCF 客户端出现“未为目标提供服务证书”错误
【发布时间】:2012-04-16 01:41:26
【问题描述】:

我正在尝试使用带有证书的消息安全性在 WCF 中创建一个测试服务/客户端。我正在使用 Visual Studio 开箱即用创建的基本服务,并从我设置为客户端的另一个项目中调用它。

我创建了两个证书,一个用于服务器,另一个用于客户端,并将它们导入到我的证书存储中。我还按照以下说明操作:http://msdn.microsoft.com/en-us/library/ms733098.aspx

但是,没有运气。从客户端调用服务器时出现错误:

没有为目标“http://localhost:1704/Service1.svc”提供服务证书。在 ClientCredentials 中指定服务证书。

我的服务配置如下:

<system.serviceModel>
    <services>
      <service name="WcfService2.Service1" behaviorConfiguration="ServiceCredentialsBehavior">
        <endpoint address="" binding="wsHttpBinding" contract="WcfService2.IService1" bindingConfiguration="MyHTTPBindingConfig">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="MyHTTPBindingConfig">
          <security mode="Message">
            <message clientCredentialType="Certificate" negotiateServiceCredential="false" establishSecurityContext="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceCredentialsBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="WCFTest" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

我的客户端配置是:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Certificate" negotiateServiceCredential="false"
                        algorithmSuite="Default" establishSecurityContext="false" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:1704/Service1.svc" binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="WSHttpBinding_IService1" behaviorConfiguration="endpointCredentialBehaviours">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
  <behaviors>
    <endpointBehaviors>
      <behavior name="endpointCredentialBehaviours">
        <clientCredentials>
          <clientCertificate findValue="WCFClient" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName"/>
        </clientCredentials>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

我在客户端调用服务:

    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    string s = client.GetData(1);
    label1.Text = s;
    client.Close();

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    这是一个有效的客户端配置示例:

    <client>
     <endpoint address="http://example.com/Myservice.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
            contract="Core.IService" name="WSHttpBinding_IService" behaviorConfiguration="myServiceBehaviour" >
       <identity>
        <dns value="SampleServiceCertificate"/>
       </identity>
     </endpoint>
    </client>
        <behaviors>
          <endpointBehaviors>
            <behavior name="myServiceBehaviour">
              <clientCredentials>
                <serviceCertificate>
                  <defaultCertificate storeLocation="LocalMachine" storeName="My" findValue="SampleServiceCertificate" x509FindType="FindBySubjectName"  />
                </serviceCertificate>
              </clientCredentials>
            </behavior>        
          </endpointBehaviors>      
        </behaviors>
    

    在您发布的配置中,clientCredentials 节点缺少 serviceCertificate 子节点。

    【讨论】:

    • &lt;defaultCertificate&gt; 中的 &lt;serviceCertificate&gt; 元素解决了我的问题。
    【解决方案2】:

    正如错误所暗示的,您的客户似乎没有提供证书。为了解决这个问题,我要做的第一步是确保您的客户端证书在您需要的位置,并且配置文件中的名称是正确的。您可以使用 MMC 做到这一点。以下是有关如何执行此操作的说明:

    如何:使用 MMC 管理单元查看证书: http://msdn.microsoft.com/en-us/library/ms788967.aspx

    我也会尝试通过代码手动添加客户端证书:

    如何:指定客户端凭据值: http://msdn.microsoft.com/en-us/library/ms732391.aspx

    【讨论】:

    • 感谢您的回答,我确实在代码和 web.config 中指定了客户端凭据,都产生了相同的错误。不幸的是,我现在已经转向其他事情,所以我可能永远不会知道答案。
    【解决方案3】:

    您必须在client.Credentials 中提供证书。更多详细解释请关注this resource

    【讨论】:

      【解决方案4】:

      我知道这是一个相当古老的线程。但我认为这可能会帮助面临这个问题的其他人。

      根据问题,您需要向客户端安装服务级别证书,该证书基本上是服务建立信任所必需的。请查看以下链接以供参考:

      https://msdn.microsoft.com/en-us/library/ms731058(v=vs.110).aspx

      https://msdn.microsoft.com/en-us/library/ms752233(v=vs.110).aspx

      将证书添加到存储后,在服务配置文件中添加以下设置:

       <serviceCredentials>
            <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
          </serviceCredentials>
      

      以上链接提供了完整的配置设置。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        • 2012-03-24
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多