简体   繁体   中英

How to make WCF NetTcpBinding working with TLS

I am new to WCF. I have a simple WCF Server/Ciient C# (Framwork 4.8) application that uses the NetTcpBinding protocol. 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.

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. 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?

You can try set the ClientCredentialType property for Transport mode,and the following code sets the property to 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

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

How to make the application working using the TLS protocol?
You can read the following articles:
Transport Layer Security (TLS) best practices with the .NET Framework
How to enable TLS 1.2 on clients

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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