如何配置WCF在互联网上使用X509证书?

问题描述 投票:37回答:2

我需要通过互联网使用X509证书来获得安全的消息级别的身份验证从富客户端的安全WCF Web服务。

具体来说,我找工作的一步一步的指导,安装,配置,编码和部署,包括建立一个“dev的”证书,安装它,并获得了生产“真正的”证书。

wcf x509
2个回答
44
投票

下面的步骤是一个指南,帮助您开始:

1)首先,你需要root权限才能生成客户端和服务器证书。您可以使用一个外部机构提供商(例如威瑞信),或者你可以生成自己的使用像Microsoft证书服务器。

要生成您可以使用“makecert”工具附带的Visual Studio,例如开发根授权证书

makecert -n "CN=MyRootCA" -r -sv RootCA.pvk RootCA.cer

2)然后,您需要请求/生成客户端和服务器证书。这两种证书可以安装为本地计算机证书,都需要使用相同的根证书颁发机构签署。您可以从Microsoft证书服务器的网络界面,例如请求客户端证书http://mycertserver/certsrv

要生成每台机器可以使用“makecert”再发展客户端证书。需要注意的是客户端证书在步骤1中创建发展的根授权证书签名。

makecert -pe -n "CN=MyCert" -ss my -sky exchange -sk MyCert 
         -iv MyRootCA.pvk -ic MyRootCA.cer -sr localmachine MyCert.cer

这将安装在其上运行命令的机器上的证书,到本地计算机存储的个人证书文件夹中。

为了使服务器信任客户端证书,你需要安装在服务器的受信任的根证书颁发机构存储发展的根授权证书(使用MMC证书管理单元来做到这一点)。该用户还应该安装在相同的方式根证书,使他们相信自己的证书。

3)配置你WCF服务使用证书(例如,通过在web.config),以要求客户机验证。

<services>
  <service
    name="TestService"
    behaviorConfiguration="wsHttpCertificateBehavior">
    <endpoint name="TestEndPoint"
      address=""
      binding="wsHttpBinding"
      bindingConfiguration="wsHttpEndpointBinding"
      contract="TestService.IMyContract">
      <identity>
        <dns value=""/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
  </service>
</services>

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding">
      <security mode="Message">
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
  <behavior name="wsHttpCertificateBehavior">
    <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
    <serviceCredentials>
      <clientCertificate>
        <authentication 
          certificateValidationMode="PeerOrChainTrust" 
          revocationMode="NoCheck"/>
      </clientCertificate>
      <serverCertificate findValue="CN=MyCert"/>
    </serviceCredentials>
  </behavior>
</behaviors>

4)现在配置呼叫者(例如,经由在app.config)。

<client>
  <endpoint name="wsHttpBinding"
    address="https://localhost/TestService/TestService.svc"
    binding="wsHttpBinding"
    bindingConfiguration="wsHttpBinding"
    behaviorConfiguration="wsHttpCertificateBehavior"
    contract="TestService.IMyContract">
    <identity>
      <dns value="MyCert"/>
    </identity>
  </endpoint>
</client>

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding">
      <security mode="Message">
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
 <endpointBehaviors>
  <behavior name="wsHttpCertificateBehavior">
    <clientCredentials>
      <clientCertificate findValue="MyCert" storeLocation="LocalMachine"/>
      <serviceCertificate>
        <authentication 
          certificateValidationMode="PeerOrChainTrust" 
          revocationMode="NoCheck" 
          trustedStoreLocation="LocalMachine"/>
      </serviceCertificate>
    </clientCredentials>
  </behavior>
 </endpointBehaviors>
</behaviors>

10
投票

我建议你阅读微软的WCF安全指导

这本书以这个场景以及许多其他

http://www.codeplex.com/WCFSecurityGuide/

编辑:现在https://archive.codeplex.com/?p=wcfsecurityguide

© www.soinside.com 2019 - 2024. All rights reserved.