【问题标题】:WCF over TLS under MonoMono 下基于 TLS 的 WCF
【发布时间】:2019-09-22 08:48:45
【问题描述】:

我正在尝试让 WCF 通信通过 TLS 连接工作。我在 Ubuntu 18.04 上使用 Mono 5.20.1.19,尽管我希望解决方案也能在 Windows 上运行。

考虑这样的基本界面:

IExample.cs:

using System;
using System.ServiceModel;

namespace Example
{
    [ServiceContract]
    public interface IExample
    {
        [OperationContract]
        string Greet();
    }
}

我有一个服务器为接口的实现设置 ServiceHost:

Server.cs:

using System;
using System.Net;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Security.Cryptography.X509Certificates;

namespace Example
{
    public class ExampleImpl : IExample
    {
        public string Greet()
        {
            Console.WriteLine("Greet() called");

            return "Hello!";
        }
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            using(var host = new ServiceHost(typeof(ExampleImpl), new Uri("net.tcp://localhost:5555"))){
                var binding = new NetTcpBinding(SecurityMode.Transport);
                binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
                binding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;

                host.Credentials.ServiceCertificate.SetCertificate(
                    StoreLocation.CurrentUser,
                    StoreName.My,
                    X509FindType.FindBySubjectName,
                    "server");

                host.AddServiceEndpoint(typeof(IExample), binding, "Example");

                host.Open();

                Console.WriteLine("listening at :5555");
                Console.WriteLine("Press Enter to end the program");

                Console.ReadLine();
            }
        }
    }
}

请注意,SecurityMode.Transport 是为 NetTcpBinding 指定的,TcpClientCredentialType.Certificate 是为客户端凭据类型指定的。除了证书的私钥之外,我还指定了一个安装到 My 证书存储区的证书。

现在是客户:

Client.cs:

using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Example
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var binding = new NetTcpBinding(SecurityMode.None);

            var factory = new ChannelFactory<IExample>(binding, new EndpointAddress("net.tcp://localhost:5555/Example"));

            var obj = factory.CreateChannel();

            Console.WriteLine(obj.Greet());
        }
    }
}

注意在客户端,NetTcpBinding的安全模式设置为None,并没有指定客户端证书。

我们可以构建两个程序:

$ csc Server.cs IExample.cs
Microsoft (R) Visual C# Compiler version 2.8.2.62916 (2ad4aabc)
Copyright (C) Microsoft Corporation. All rights reserved.
$ csc Client.cs IExample.cs
Microsoft (R) Visual C# Compiler version 2.8.2.62916 (2ad4aabc)
Copyright (C) Microsoft Corporation. All rights reserved.

现在,如果我们运行 Server.exe,让它保持打开状态,然后在另一个会话中运行 Client.exe,服务器会打印消息 Greet() called,而客户端会打印 Hello!

我的困惑是为什么连接成功。我希望既然服务器的绑定设置为Transport,那么它应该需要一个TLS连接;但是似乎没有使用 TLS,因为没有指定客户端证书。

如何将代码的服务器部分更改为需要 TLS 连接?

【问题讨论】:

    标签: c# wcf ssl tcp


    【解决方案1】:

    首先,我无法重现您的问题,我在本地调用服务时出现以下错误。

    然后,NetTcpBinding 可以使用 Windows 凭据或证书来确保传输层安全。
    请参考我之前回复过的帖子。
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7b300a92-8533-4afe-9e8b-3f38138dd23b/ssltsl-with-nettcpbinding?forum=wcf

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2017-01-03
      • 2021-01-15
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      相关资源
      最近更新 更多