C# 使用服务器和客户端证书的WCF加密/解密
我已经创建了一个服务,其中在我托管该服务的服务器上有一个私人证书,客户端将拥有该证书的公钥。 客户机将拥有一个不同的私钥,他们将加密发送到我创建的端点的消息,并且我拥有用于解密消息的公钥。 到目前为止,我在服务器配置文件中的内容 因此,这一个负责托管服务的主私有证书。我不确定在客户端拥有/使用私钥加密消息的位置/如何放置证书的公钥 任何帮助都将不胜感激C# 使用服务器和客户端证书的WCF加密/解密,c#,wcf,x509certificate,C#,Wcf,X509certificate,我已经创建了一个服务,其中在我托管该服务的服务器上有一个私人证书,客户端将拥有该证书的公钥。 客户机将拥有一个不同的私钥,他们将加密发送到我创建的端点的消息,并且我拥有用于解密消息的公钥。 到目前为止,我在服务器配置文件中的内容 因此,这一个负责托管服务的主私有证书。我不确定在客户端拥有/使用私钥加密消息的位置/如何放置证书的公钥 任何帮助都将不胜感激 <?xml version="1.0"?> <configuration> <appSettings>
<?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>
非常好,您对SSL证书的工作机制有了深入的了解。请参考下面的链接。 客户端和服务器端在通信期间自动协商证书的公钥,以使用另一方的公钥加密消息,并使用私钥解密soap消息。因此,我们不需要手动编程这个过程。在本地证书存储中安装彼此的证书就足够了。 如果使用消息安全模式对客户端进行身份验证,则需要使用服务凭据部分来配置服务证书。就像你做的那样
<serviceCredentials>
<serviceCertificate findValue="0000xx000" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySerialNumber"/>
<clientCertificate>
<authentication certificateValidationMode="PeerOrChainTrust"/>
</clientCertificate>
</serviceCredentials>
至于证书之间的信任关系,在客户端,我们需要在LocalCA中安装服务器证书,在服务器端,我们需要根据身份验证模式在特定位置安装客户端证书。默认情况下,可以在LocalCA中安装它
如果有什么我可以帮忙的,请随时告诉我。非常感谢您的回复。
//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;
}
//this is default authentication mode.
sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode= System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;