WCF 身份验证的 SSL 证书

发布于 2024-11-29 06:11:37 字数 3104 浏览 3 评论 0原文

我开发了一个应用程序,它启动多个 WCF 服务,这些服务使用 SecurityMode.Message 来加密通信。

它可以工作,但是非常复杂,因为我们必须生成 SSL 证书并将其放在服务器和客户端上的特定存储中。

问题是,将使用该程序的客户:

  • 不在域中(事实上,服务器肯定在域中,但客户端不在域中
  • 不想购买证书

那么我最好的选择是什么?我只需要加密数据,我不需要确保连接到正确的主机,

我知道我不是在最好的情况下,但该应用程序将被某些特定用户使用

。我建立连接的代码:

服务器side:

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();

请注意,我删除了有关自定义身份验证的部分以获得更干净的代码

I've developed an application which starts several WCF Service which use the SecurityMode.Message to encrypt the communication.

It's working, but, it's very complicate, because we have to generate a SSL cert and put it in specific store, on the server and on the client.

The problem is that the customer which will use the program:

  • Is not in a domain(in fact, the server will certainly be in a domain, but not the client
  • Doesn't want to buy a cert

So what is my best shot? I only need to encrypt data, I don't need to ensure that I'm connecting to the right host.

I know I'm not in the best case, but the application will be used by some specific users.

Here is a part of my code which makes the connection:

Server side:

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));

Client side:

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();

Please note that I removed the part concerning my custom auth to have a more clean code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2024-12-06 06:11:37

您的客户想要 Windows 域之外的安全 = 您的客户想要证书。没有证书=没有安全。这就是您必须向客户解释的内容。

您只需要在服务器上拥有带有私钥的证书,并且客户端必须信任该证书(如果信任发布者,则不必安装它)。这是什么意思?您有三个选择:

  • 您的客户必须从受信任的发布者那里购买证书,您的客户端将简单地工作
  • 您的客户必须安装自己的证书颁发机构,它将生成证书,并且客户端必须在其受信任的根颁发机构存储中拥有颁发机构的证书(每个更大的证书颁发机构)公司有自己的证书颁发机构)。
  • 您将在生产中使用自签名证书。这是“不太安全”并且不推荐。安全性较低意味着您的客户端必须信任自签名证书,并且它无法验证证书链=它无法验证该证书是由受信任的颁发机构颁发的,并且无法验证该证书颁发机构已吊销受损的证书。在这种情况下,您必须在每个客户端上安装服务证书 - 这是信任自签名证书的唯一方法(安装本身实际上就是信任)。

这就是安全的运作方式。您可以构建自己的 - 您将为此付出巨大的努力,但最终您仍然需要带有非对称加密的 PKI(私钥基础设施)以使其真正安全。证书主要是关于包装、存储和传输公钥和私钥。

Your customer wants security outside of windows domain = your customer wants certificate. No certificate = no security. That is what you must explain to your customer.

You just need to have certificate with private key on the server and client must trust that certificate (it doesn't have to install it if it trust a publisher). What does it mean? You have three options:

  • Your customer must buy certificate from trusted publisher and your clients will simply work
  • Your customer must have its own certificate authority installed which will generate the certificate and clients must have certificate of the authority in their trusted root authorities store (every bigger company has its own certificate authority).
  • You will use self signed certificate in the production. This is "less secure" and not recommended. The less secure means that your client must trust self signed certificate and it cannot validate certificate chain = it cannot validate that certificate was issued by trusted authority and it cannot validate that certificate authority revoked compromised certificate. In this scenario you must install service certificate on each client - it is the only way to trust self signed certificate (installing itself is actually the trust).

That is the way how security works. You can build your own - you will put a big effort in that but at the end you will still need PKI (private key infrastructure) with asymmetric encryption to make it really secure. Certificates are mostly about wrapping, storing and transferring public and private keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文