【问题标题】:How to make WCF NetTcpBinding working with TLS如何使 WCF NetTcpBinding 与 TLS 一起使用
【发布时间】:2022-01-06 18:18:12
【问题描述】:

我是 WCF 的新手。 我有一个使用 NetTcpBinding 协议的简单 WCF Server/Ciient C# (Framwork 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 协议工作?

【问题讨论】:

    标签: c# wcf ssl


    【解决方案1】:

    您可以尝试将 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 协议工作?
    您可以阅读以下文章:
    Transport Layer Security (TLS) best practices with the .NET Framework
    How to enable TLS 1.2 on clients

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2013-01-19
      • 2010-09-29
      • 2020-03-14
      相关资源
      最近更新 更多