使用本地证书测试WCF服务

时间:2019-06-12 16:08:36

标签: wcf certificate

我有一个WCF服务,它公开了一种接收内容的方法。客户端将通过互联网使用此服务。客户端提供了以下证书,并按如下所示在我的本地计算机上安装了它们:

Comodo中级.cert 1)中级认证机构> Comodo中级

Comodo Root .cert 2)受信任的根证书颁发机构> Commodo Root

X509客户端证书.pem 3)Trusted People Store>客户证书

我想测试/模拟客户端调用以测试本地运行的Web服务。我安装了证书,并将以下绑定添加到我的WCF服务配置

<protocolMapping>
      <add scheme="https" binding="wsHttpBinding"/>
    </protocolMapping>
    <bindings>
      <wsHttpBinding>
        <binding>
          <security mode="Transport">
            <transport clientCredentialType="Certificate"></transport>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

我创建了一个测试客户端控制台应用程序,并添加了以下配置

<behaviors>
      <endpointBehaviors>
        <behavior name="endpointCredentialBehavior">
          <clientCredentials>
            <clientCertificate findValue="ClientCertificate"
                               storeLocation="LocalMachine"
                               storeName="My"
                               x509FindType="FindBySubjectName" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
          <security mode="Transport">
            <transport clientCredentialType="Certificate"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

我知道在测试和生产环境中,我具有服务器证书,但是要在本地成功地进行所有测试,是否需要创建服务器证书以及如何创建。可以在同一盒子上完成此操作,还是必须使用SOAP UI或其他功能?

1 个答案:

答案 0 :(得分:0)

如果您有服务证书(由某个正式机构颁发),则可以在本地进行测试,请参阅以下链接。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
当我们使用带有证书的传输安全性时,我们应该首先在客户端和服务器之间建立信任关系,然后,如果我们要使用自签名证书,则可以使用PowerShell来创建证书。请参考下面的Powershell命令创建自签名证书。

New-SelfSignedCertificate -DnsName "vabqia864VM" -CertStoreLocation "cert:\LocalMachine\My"

有关详细信息。
https://docs.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps
对于服务器端,由于我们使用https协议,因此应该使用SSL证书配置端口(如果使用IIS托管,则网站绑定模块将执行此操作)。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
对于客户端,我们应该提供用于身份验证的客户端证书(也可以使用端点行为来完成此操作)。

ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "9ee8be61d875bd6e1108c98b590386d0a489a9ca");

请随时告诉我是否有什么可以帮忙的。