WCF-消息凭据与TransportWithMessageCredential证书验证

WCF-消息凭据与TransportWithMessageCredential证书验证,wcf,ssl,certificate,x509,Wcf,Ssl,Certificate,X509,我在设置WCF服务消息凭据安全性时遇到了一个有趣的问题。我在我的客户端遇到了以下异常: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll Additional information: The service certificate is not provided for target 'http://myMachine/SPTestService/Service

我在设置WCF服务消息凭据安全性时遇到了一个有趣的问题。我在我的客户端遇到了以下异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: The service certificate is not provided for target 'http://myMachine/SPTestService/Service1.svc'. Specify a service certificate in ClientCredentials. 
这使我相信我需要在我的客户端配置中指定服务器证书,但我不知道为什么。这应该使用ChainTrust。有趣的是,当我将它切换到TransportWithMessageCredential(因此它在SSL上工作)时,它工作了,并且正确地验证了消息凭证。这是一个WCF错误吗?幸运的是,TransportWithMessageCredential正是我要去的地方,所以我将加快这个过程

仅使用消息凭据,我的配置如下所示:

客户:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="serviceBehavior">
                    <clientCredentials>
                        <clientCertificate storeName="My" storeLocation="LocalMachine"
                                           findValue="CN=myCert" />
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="serviceEndpoint" 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="Mtom" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Message">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Certificate" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://myMachine/SPTestService/Service1.svc"
                behaviorConfiguration="serviceBehavior"      
                binding="basicHttpBinding" bindingConfiguration="serviceEndpoint"
                contract="ServiceReference2.IService1" name="serviceEndpoint" />
        </client>
    </system.serviceModel>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="serviceBehavior">
                    <clientCredentials>
                        <clientCertificate storeName="My" storeLocation="LocalMachine"
                                           findValue="CN=myCert" />
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="serviceEndpoint" 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="Mtom" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="TransportWithMessageCredential">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Certificate" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://myMachine/SPTestService/Service1.svc"
                behaviorConfiguration="serviceBehavior"      
                binding="basicHttpBinding" bindingConfiguration="serviceEndpoint"
                contract="ServiceReference2.IService1" name="serviceEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

服务:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>  
  <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serverBinding" messageEncoding="Mtom">
                <security mode="Message">
                    <message clientCredentialType="Certificate" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />
          <serviceCredentials>
            <serviceCertificate storeName="My" storeLocation="LocalMachine" findValue="CN=myCert"  />
            <clientCertificate>
                <authentication revocationMode="NoCheck"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="serviceBehavior" name="SPTestService.Service1">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="serverBinding" name="serviceEndpoint" contract="SPTestService.IService1" />
          </service>
      </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>  
  <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serverBinding" messageEncoding="Mtom">
                <security mode="TransportWithMessageCredential">
                    <message clientCredentialType="Certificate" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />
          <serviceCredentials>
            <serviceCertificate storeName="My" storeLocation="LocalMachine" findValue="CN=myCert"  />
            <clientCertificate>
                <authentication revocationMode="NoCheck"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="serviceBehavior" name="SPTestService.Service1">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="serverBinding" name="serviceEndpoint" contract="SPTestService.IService1" />
          </service>
      </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

使用TransportWithMessage,我的配置如下所示:

客户:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="serviceBehavior">
                    <clientCredentials>
                        <clientCertificate storeName="My" storeLocation="LocalMachine"
                                           findValue="CN=myCert" />
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="serviceEndpoint" 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="Mtom" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Message">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Certificate" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://myMachine/SPTestService/Service1.svc"
                behaviorConfiguration="serviceBehavior"      
                binding="basicHttpBinding" bindingConfiguration="serviceEndpoint"
                contract="ServiceReference2.IService1" name="serviceEndpoint" />
        </client>
    </system.serviceModel>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="serviceBehavior">
                    <clientCredentials>
                        <clientCertificate storeName="My" storeLocation="LocalMachine"
                                           findValue="CN=myCert" />
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="serviceEndpoint" 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="Mtom" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="TransportWithMessageCredential">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Certificate" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://myMachine/SPTestService/Service1.svc"
                behaviorConfiguration="serviceBehavior"      
                binding="basicHttpBinding" bindingConfiguration="serviceEndpoint"
                contract="ServiceReference2.IService1" name="serviceEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

服务:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>  
  <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serverBinding" messageEncoding="Mtom">
                <security mode="Message">
                    <message clientCredentialType="Certificate" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />
          <serviceCredentials>
            <serviceCertificate storeName="My" storeLocation="LocalMachine" findValue="CN=myCert"  />
            <clientCertificate>
                <authentication revocationMode="NoCheck"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="serviceBehavior" name="SPTestService.Service1">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="serverBinding" name="serviceEndpoint" contract="SPTestService.IService1" />
          </service>
      </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>  
  <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serverBinding" messageEncoding="Mtom">
                <security mode="TransportWithMessageCredential">
                    <message clientCredentialType="Certificate" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />
          <serviceCredentials>
            <serviceCertificate storeName="My" storeLocation="LocalMachine" findValue="CN=myCert"  />
            <clientCertificate>
                <authentication revocationMode="NoCheck"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="serviceBehavior" name="SPTestService.Service1">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="serverBinding" name="serviceEndpoint" contract="SPTestService.IService1" />
          </service>
      </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>