我开发了一个启动多个 WCF 服务的应用程序,这些服务使用 SecurityMode.Message 来加密通信。
它可以工作,但是非常复杂,因为我们必须生成一个 SSL 证书并将其放在特定的存储区、服务器上和客户端上。
问题在于将使用该程序的客户:
- 不在域中(实际上服务器肯定会在域中,但客户端不会)
- 不想购买证书
那么我最好的镜头是什么?我只需要加密数据,不需要确保连接到正确的主机。
我知道我不是最好的情况,但该应用程序将被某些特定用户使用。
这是我建立连接的代码的一部分:
服务器端:
ServiceHost host = new ServiceHost(typeof(MyServiceType))
WSHttpBinding binding = new WSHttpBinding
{
ReaderQuotas = { MaxStringContentLength = int.MaxValue, MaxArrayLength = int.MaxValue, MaxDepth = int.MaxValue, MaxBytesPerRead = int.MaxValue, MaxNameTableCharCount = int.MaxValue },
MaxReceivedMessageSize = int.MaxValue
};
TimeSpan timeoutSpan = TimeSpan.FromMilliseconds(timeout);
binding.CloseTimeout = timeoutSpan;
binding.OpenTimeout = timeoutSpan;
binding.ReceiveTimeout = timeoutSpan;
binding.SendTimeout = timeoutSpan;
binding.ReliableSession.InactivityTimeout = timeoutSpan;
binding.MaxBufferPoolSize = int.MaxValue;
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings["Hostname"]);
host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
host
.AddServiceEndpoint(services[port], binding, String.Format("http://localhost:{0}", port));
客户端:
string remoteAddress = String.Format("{0}://{1}:{2}", Tools.GetDescription(accessInfo.ServiceHost.Protocol), accessInfo.ServiceHost.HostName, accessInfo.PortNumber);
// avoid seralization/deserialization problems with large XML's
WSHttpBinding binding = new WSHttpBinding();
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReaderQuotas.MaxDepth = int.MaxValue;
binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
TimeSpan timeoutSpan = DateTime.Now.AddMinutes(30) - DateTime.Now;
binding.CloseTimeout = timeoutSpan;
binding.OpenTimeout = timeoutSpan;
binding.ReceiveTimeout = timeoutSpan;
binding.SendTimeout = timeoutSpan;
binding.ReliableSession.InactivityTimeout = timeoutSpan;
binding.MaxBufferPoolSize = int.MaxValue;
//we set the security type
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
ChannelFactory<TService> channelFactory = new ChannelFactory<TService>(binding, remoteAddress);
_service = channelFactory.CreateChannel();
请注意,我删除了有关我的自定义身份验证的部分以获得更干净的代码
请您参考如下方法:
您的客户想要 Windows 域之外的安全性 = 您的客户想要证书。没有证书 = 没有安全性。这就是您必须向客户解释的内容。
您只需要在服务器上拥有带有私钥的证书,并且客户端必须信任该证书(如果它信任发布者则不必安装它)。这是什么意思?您有三个选择:
- 您的客户必须从受信任的发布者处购买证书,您的客户才能正常工作
- 您的客户必须安装自己的证书颁发机构来生成证书,并且客户必须在其受信任的根机构存储中拥有该机构的证书(每个大公司都有自己的证书颁发机构)。
- 您将在生产中使用自签名证书。这是“不太安全”的,不推荐。安全性较低意味着您的客户端必须信任自签名证书并且它无法验证证书链=它无法验证证书是由受信任的机构颁发的并且它无法验证证书颁发机构撤销了受损证书。在这种情况下,您必须在每个客户端上安装服务证书——这是信任自签名证书的唯一方法(安装本身实际上就是信任)。
这就是安全工作的方式。您可以构建自己的 - 您将为此付出巨大努力,但最终您仍然需要具有非对称加密的 PKI(私钥基础设施)以使其真正安全。证书主要是关于包装、存储和传输公钥和私钥。
