我有一个WCF
服务,该服务是在WSHttpBinding
中使用security mode="Message"
和clientCredentialType="None"
配置的。我在MessageHeader
上使用IClientMessageInspector
添加了自定义BeforeSendRequest
,并且我也想对其进行加密。可能吗?
编辑: 目前,我已经添加了一个自定义的不对称引擎,该引擎使用通过以下方式获取的证书进行加密:
<behaviors>
<endpointBehaviors>
<behavior>
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="Custom" customCertificateValidatorType="CustomCertificateValidator.CustomCertificateValidator, CustomCertificateValidator"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
并在将特定值写入标头/从标头读取之前,解密(在指纹找到的服务器上的证书)特定值。这是一个好方法吗?可以做得更好吗?
答案 0 :(得分:0)
这可以通过指定服务证书来完成。默认情况下,使用Windows凭据实现邮件安全性。
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
所以我们只需要指定用于加密和签名的服务证书即可。
using (ServiceHost sh = new ServiceHost(typeof(MyService)))
{
sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "cbc81f77ed01a9784a12483030ccd497f01be71c");
sh.Open();
Console.WriteLine("Service is ready....");
Console.ReadLine();
sh.Close();
}