在WCF中使用X.509证书的连接失败

时间:2011-08-26 08:06:07

标签: c# wcf certificate

如果我尝试连接到我的WCF服务,则会出现以下错误: 目标'http:// localhost:8081 / OEGNOSControlService / ws'未提供服务证书。在ClientCredentials中指定服务证书。

但我已经指定了服务证书!

服务的App.config:

<system.serviceModel> 
 <behaviors>
  <serviceBehaviors>  
  <behavior name="ServiceCredentialsBehavior">
   <serviceMetadata httpGetEnabled="true" />
   <serviceDebug includeExceptionDetailInFaults="true" />
   <serviceCredentials>
   <serviceCertificate findValue="localhost"
        storeLocation="LocalMachine"
        storeName="My"
        x509FindType="FindBySubjectName" />
   </serviceCredentials>
  </behavior>
  </serviceBehaviors>
 </behaviors> 
 <bindings>
  <wsHttpBinding>
  <binding name="MessageAndUserName">
   <security mode="Message">
   <message clientCredentialType="UserName" negotiateServiceCredential="false"
    algorithmSuite="Default" establishSecurityContext="true" />
   </security>
  </binding>
  </wsHttpBinding>
 </bindings>
 <services>
  <service behaviorConfiguration="ServiceCredentialsBehavior"
    name="OEGNOS.ControlServiceLibrary.ControlService">
  <clear/>
  <endpoint
   name="WSHttpBinding_IControlService"
   address="ws"
     binding="wsHttpBinding"
     bindingConfiguration="MessageAndUserName"
     contract="OEGNOS.ControlServiceLibrary.IControlService" />
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  <host>
   <baseAddresses>
   <add baseAddress="http://localhost:8081/OEGNOSControlService/" />
   </baseAddresses>
  </host>
  </service>
 </services>
 </system.serviceModel>

客户端的app.config:

<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="ClientCredentialsBehavior">
     <clientCredentials>
      <serviceCertificate>
       <authentication certificateValidationMode="PeerOrChainTrust" />
      </serviceCertificate>
     </clientCredentials>
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <bindings>
   <wsHttpBinding>
    <binding name="WSHttpBinding_IControlService" 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">
      <message clientCredentialType="UserName" negotiateServiceCredential="false"
       algorithmSuite="Default" establishSecurityContext="true" />
     </security>
    </binding>    
   </wsHttpBinding>
  </bindings>
  <client>
   <endpoint address="http://localhost:8081/OEGNOSControlService/ws"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IControlService"
    contract="ServiceReference.IControlService" name="WSHttpBinding_IControlService">
    <identity>
     <certificate encodedValue="AwAAAAEAAAAUAAAA76QJWw679+g8dC71G0FPkVBqP84gAAAAAQAAALUBAAAwggGxMIIBX6ADAgECAhC+qdh0bQjNsUbkHmHD4dv1MAkGBSsOAwIdBQAwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3kwHhcNMTEwODI0MDcxNzIzWhcNMzkxMjMxMjM1OTU5WjAUMRIwEAYDVQQDEwlsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALhYR3qBO1xZlHJux2pTPWnwltvXzhtIyQJJXJwaNoyJz1rAr6LsTOxXbjXXRB4TV74TjgTzVtY8z/bkJhjexBmrq/79myw1y1RZegOSF64p4oYlQX2+uw/AlGgP+VBDgHWamVK6Fdansn/9kQRHRHbfK/DJLdBCzWWHj3tR+jiVAgMBAAGjSzBJMEcGA1UdAQRAMD6AEBLkCS0GHR1PAI1hIdwWZGOhGDAWMRQwEgYDVQQDEwtSb290IEFnZW5jeYIQBjdsAKoAZIoRz7jUqlw19DAJBgUrDgMCHQUAA0EAITzB2uIn2MuJ11s1uXwNtPOESTrfKlVIfrccGzkuggUJgAiHEQkj/1PwQN+Q47wD5aWYzjUN5CCVMefD9djBYA==" />
    </identity>
   </endpoint>   
  </client>
 </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

尝试在客户端上安装证书并使用

更新客户端配置
<behavior name="ClientCredentialsBehavior">
     <clientCredentials>
      <serviceCertificate>
         <!-- path for client certificate-->
        <defaultCertificate findValue="localhost"
            storeLocation="LocalMachine"
            storeName="My"
            x509FindType="FindBySubjectName"/>
       <authentication certificateValidationMode="None" />
      </serviceCertificate>
     </clientCredentials>
</behavior>

如果你这样做,就不会有例外。请注意storeLocation值,默认情况下导入后为CurrentUser(不是LocalMachine)。你可以使用'cmd - &gt;看到它。 mmc - &gt;证书。

实际上,您不必安装证书,只需发送给客户certificate encodedValue即可。为此,请不要使用svcutil.exe(通过单击Visual Studio中的“添加服务引用”来使用它)。

相反,

  1. 使用mmc util在证书库中查找证书。
  2. 右键单击证书 - &gt;出口。
  3. 选择“不导出私钥”和base-64编码。
  4. 使用文本编辑器保存打开文件后,您将获得编码值。
  5. 将找到的编码值放在客户端配置identity section中。