2011-10-01 107 views
3

我试图让托管在IIS将使用X.509证书,邮件安全和与需要相互验证SSL运输安全WCF4服务(项目规范要求使用WS-Security SSL,AFAIK只有一个通常是要走的路,但没关系)。WCF 4 - TransportWithMessageCredential使用X.509证书运输和信息安全

我做了我TestRootCA并用它来颁发服务器证书(本地主机)和客户端证书(TestUser用户)。我首先只想建立和测试传输安全性,所以我配置IIS使用https,配置的SSL证书(我制作的localhost证书),并将IIS配置为,要求客户端提供客户端证书。然后我修改了服务web.config,创建了相应的svcutil.conf,并运行了svcutil,成功为我的测试WinForms应用程序创建了客户端代理类和app.config。

var client = new ServiceClient(); 
client.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser, System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectDistinguishedName, "CN=TestUser"); 
MessageBox.Show(client.GetData(42)); 

到目前为止好:我通过调用创建代理客户端时设置的证书。但是,当我修改服务的web.config以使用TrasportWithMessageCredential(而不是传输)并指定“Certificate”作为clientCredentialType以获得邮件安全性时,我获得HTTP 403 - The HTTP request was forbidden with client authentication scheme 'Anonymous',即使我指定了“Certificate”。

我最后的web.config文件看起来是这样的:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="wsHttpEndpointBinding"> 
       <security mode="TransportWithMessageCredential"> 
        <transport clientCredentialType="Certificate"/> 
        <message clientCredentialType="Certificate"/> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="serviceBehavior" name="Service"> 
      <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="wsHttpEndpoint" contract="IService" /> 
      <endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="mexEndpoint" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="serviceBehavior"> 
       <serviceMetadata httpsGetEnabled="true" /> 
       <serviceDebug includeExceptionDetailInFaults="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
</system.serviceModel> 

和我的客户app.conf:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="wsHttpEndpoint" 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="TransportWithMessageCredential"> 
        <transport clientCredentialType="Certificate"/> 
        <message clientCredentialType="Certificate"/> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://localhost/WCFTestService/Service.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpoint" contract="IService" name="wsHttpEndpoint" /> 
    </client> 
</system.serviceModel> 

任何想法?

编辑:

我已经成功地得到它通过更改IIS设置SSL客户端证书的工作从需要接受。看来,当我介绍了使用SSL使用过的运输安全证书信息安全,客户端使用通过SSL相互认证的证书,邮件签名,但(它试图身份登录,即使它有指定的证书匿名 )。

不知道为什么发生这种情况,我想从专家:)一些评论。

回答

1

如问题所说,我把它改变IIS设置SSL客户端证书从requireaccept工作。然后在服务入口点,我编程检查远程用户的证书(如果它不为空并且有效)。

1

必须指定证书使用该消息中的行为部分加密,因为这些可能是不同的,因为在那些用于建立HTTPS通道

它看起来像这样在服务器

<system.serviceModel> 
<behaviors>  
    <serviceBehaviors>  
     <behavior name="serviceBehavior"> 
     <serviceCredentials> 
     <serviceCertificate findValue="ServerCertificate" 
          storeLocation="CurrentUser" 
          storeName="My" 
          x509FindType="FindByIssuerName" /> 
     <clientCertificate> 
      <certificate findValue ="ClientCertificate" 
         storeLocation="CurrentUser" 
          storeName="My" 
          x509FindType="FindByIssuerName"/> 
      <authentication certificateValidationMode ="PeerTrust"/> 
     </clientCertificate> 
     </serviceCredentials> 
      <serviceMetadata httpsGetEnabled="true" />  
      <serviceDebug includeExceptionDetailInFaults="true" />  
     </behavior>  
    </serviceBehaviors>  
</behaviors> 
</system.serviceModel> 

,做在客户端一样的,那应该是这样的

<system.serviceModel>   
    <bindings>   
     <wsHttpBinding>   
      <binding name="wsHttpEndpoint" 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="TransportWithMessageCredential">   
        <transport clientCredentialType="Certificate"/>   
        <message clientCredentialType="Certificate"/>   
       </security>   
      </binding>   
     </wsHttpBinding>   
    </bindings> 
<behaviors>  
    <endpointBehaviors>   
    <behavior name="ClientCredentialsBehavior"> 
     <clientCredentials>    
     <clientCertificate x509FindType="FindBySubjectName" 
          findValue="ClientCertificate" 
          storeLocation="CurrentUser" 
          storeName="My"/> 
     <serviceCertificate> 
      <defaultCertificate x509FindType="FindBySubjectName" 
           findValue="ServerCertificate" 
           storeLocation="CurrentUser" 
           storeName="My"/> 
     </serviceCertificate> 
     </clientCredentials> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

    <client>   
     <endpoint address="https://localhost/WCFTestService/Service.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpoint" contract="IService" name="wsHttpEndpoint" behaviorConfiguration="ClientCredentialsBehavior" />   
    </client>   
</system.serviceModel> 

关当然,你必须设置证书的正确位置和名称证书,在服务器和客户端。

我希望这可以帮助仍然

+0

还没有尝试过这一点,但即使它能够工作,在* server * config中指定* client *证书的需求也是一个交易断路器。服务器事先并不知道客户端证书(并且有许多不同的客户端),它使用链式信任(如果客户端证书的有效性是有效的,那么客户端证书被接受并用作标识)。 –

+0

这解决了我得到的错误“HTTP请求被客户端身份验证方案'Anonymous'禁止”。我只有指定的服务器证书。谢谢 – Nanook