简体   繁体   English

如何使 WCF NetTcpBinding 与 TLS 一起使用

[英]How to make WCF NetTcpBinding working with TLS

I am new to WCF.我是 WCF 的新手。 I have a simple WCF Server/Ciient C# (Framwork 4.8) application that uses the NetTcpBinding protocol.我有一个使用 NetTcpBinding 协议的简单 WCF 服务器/客户端 C#(框架 4.8)应用程序。 The application sends a message to the Server, and Server returns the message back with a datetime stamp.应用程序向服务器发送一条消息,服务器返回带有日期时间戳的消息。

I need to make the application working with TLS.我需要使应用程序使用 TLS。

Server:服务器:

host = new ServiceHost(typeof(MyService));

NetTcpBinding binding = new NetTcpBinding();

binding.Security.Mode = SecurityMode.Transport;

binding.Security.Transport.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | 
SslProtocols.Tls;

host.AddServiceEndpoint(typeof(IMyService), binding, new Uri("net.tcp://localhost:8888/implementclass"));

host.Open();

Client:客户:

NetTcpBinding binding = new NetTcpBinding();    

binding.Security.Mode = SecurityMode.Transport;

binding.Security.Transport.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; 

EndpointAddress epa = new EndpointAddress($"net.tcp://{txtIPAddress.Text}:8888/implementclass");

ChannelFactory<IMyService> chn = new ChannelFactory<IMyService>(binding, epa);

chn.CreateChannel();

ServiceContract:服务合同:

[OperationContract] [运营合同]

string Send(string s);

When Client/Server run on two different computers ( Firewall is disabled on both), the following error appers:当客户端/服务器在两台不同的计算机上运行(防火墙在两台计算机上都禁用)时,出现以下错误:

The server has rejected the client credentials服务器已拒绝客户端凭据

The Client/Server work on the same PC installed.客户端/服务器在安装的同一台 PC 上工作。 Also the Client/Server work when I am using unsecure connection:当我使用不安全的连接时,客户端/服务器也可以工作:

binding.Security.Mode = SecurityMode.None

How to make the application working using the TLS protocol?如何使应用程序使用 TLS 协议工作?

You can try set the ClientCredentialType property for Transport mode,and the following code sets the property to Windows.您可以尝试为传输模式设置 ClientCredentialType 属性,以下代码将该属性设置为 Windows。

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType =TcpClientCredentialType.Windows;

https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.nettcpbinding?view=dotnet-plat-ext-6.0 https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.nettcpbinding?view=dotnet-plat-ext-6.0

<security mode="Transport">
      <transport clientCredentialType="Windows" />
</security>

How to make the application working using the TLS protocol?如何使应用程序使用 TLS 协议工作?
You can read the following articles:您可以阅读以下文章:
Transport Layer Security (TLS) best practices with the .NET Framework .NET 框架的传输层安全 (TLS) 最佳实践
How to enable TLS 1.2 on clients 如何在客户端上启用 TLS 1.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM