簡體   English   中英

如何使 WCF NetTcpBinding 與 TLS 一起使用

[英]How to make WCF NetTcpBinding working with TLS

我是 WCF 的新手。 我有一個使用 NetTcpBinding 協議的簡單 WCF 服務器/客戶端 C#(框架 4.8)應用程序。 應用程序向服務器發送一條消息,服務器返回帶有日期時間戳的消息。

我需要使應用程序使用 TLS。

服務器:

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

客戶:

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

服務合同:

[運營合同]

string Send(string s);

當客戶端/服務器在兩台不同的計算機上運行(防火牆在兩台計算機上都禁用)時,出現以下錯誤:

服務器已拒絕客戶端憑據

客戶端/服務器在安裝的同一台 PC 上工作。 當我使用不安全的連接時,客戶端/服務器也可以工作:

binding.Security.Mode = SecurityMode.None

如何使應用程序使用 TLS 協議工作?

您可以嘗試為傳輸模式設置 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

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

如何使應用程序使用 TLS 協議工作?
您可以閱讀以下文章:
.NET 框架的傳輸層安全 (TLS) 最佳實踐
如何在客戶端上啟用 TLS 1.2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM