使用服务器和客户端证书的WCF加密/解密

时间:2020-01-07 21:49:29

标签: c# wcf x509certificate

我创建了一个服务,该服务器将在托管该服务的服务器上有一个私有证书,并且客户端将拥有该服务器的公钥。 客户端将拥有一个不同的私钥,他们将在其中加密发送到我创建的端点的消息,并拥有用于解密消息的公钥。 到目前为止,我在服务器配置文件中所拥有的。

因此,该托管服务器将托管服务的主要私有证书。我不确定客户端将证书的公共密钥放在何处/如何将其放置在何处/使用私钥对邮件进行加密。

任何帮助将不胜感激。

<?xml version="1.0"?>
<configuration>
  <appSettings>
  </appSettings>
  <system.web>
    <httpRuntime maxRequestLength="2147483647"/>
    <compilation debug="false" strict="false" explicit="true" targetFramework="4.5.2"/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
    <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpEndPointBinding">
          <security mode="Message">
            <message clientCredentialType="Certificate"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="wcfJNet.ServiceBehavior" name="wcfJNetService">
        <endpoint address="" binding="basicHttpBinding" 
          bindingConfiguration="basicHttpEndPointBinding"
          contract="IJNetService">
          <identity>
            <dns value="xxxxxx" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="wcfJNet.ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <serviceCertificate findValue="0000xx000" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySerialNumber"/>
            <clientCertificate>
              <authentication certificateValidationMode="PeerOrChainTrust"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

1 个答案:

答案 0 :(得分:1)

非常好,您对SSL证书的工作机制有深刻的了解。请参考以下链接。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/message-security-with-a-certificate-client
客户端和服务器端在通信期间自动协商证书的公钥,以使用对方的公钥对消息进行加密,并使用私钥对肥皂消息进行解密。因此,我们无需手动编程此过程。在本地证书存储区中安装彼此的证书就足够了。
如果我们使用消息安全性模式对客户端进行身份验证,则需要使用service credential部分来配置服务证书。就像您所做的一样。

<serviceCredentials>
            <serviceCertificate findValue="0000xx000" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySerialNumber"/>
            <clientCertificate>
              <authentication certificateValidationMode="PeerOrChainTrust"/>
            </clientCertificate>
          </serviceCredentials>

通常,在客户端,我们需要指定两个证书,一个是服务证书,另一个是客户端证书。

//message security, we need to specify both the default certificate and the client certificate.
            ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();            client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, "cbc81f77ed01a9784a12483030ccd497f01be71c");
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "9b8db0dfe615458ace0ae9e89fcb983c5d16f633");
            try
            {
                var result = client.SayHello();
                Console.WriteLine(result);
            }
            catch (Exception)
            {
                throw;
            }

关于证书之间的信任关系,在客户端,我们需要在LocalCA中安装服务器证书,而在服务器端,我们需要在特定位置安装客户端证书位置取决于身份验证模式。默认情况下,可以将其安装在LocalCA中。

    //this is default authentication mode. 
  sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode= System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;

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

相关问题