【问题标题】:Validate SSL Certificate验证 SSL 证书
【发布时间】:2019-03-29 21:08:37
【问题描述】:

我正在尝试通过 tls/ssl 将我的 c# 客户端连接到 node.js 服务器。服务器正在工作,我已经创建了我的证书。但是如果我想通过 TcpClient 连接到服务器,它会说证书无效。您可以自己验证该证书吗?大家有什么想法吗?

namespace Client
{
    class Program
    {
        private static TcpClient _TcpClient = new TcpClient("localhost", 8000);
        private static SslStream stream;
        static void Main(string[] args)
        {
            stream = new SslStream(_TcpClient.GetStream());
            stream.AuthenticateAsClient("localhost");

            byte[] buffer = new byte[2048];

            StringBuilder data = new StringBuilder();
            int bytes = -1;
            do
            {
                bytes = stream.Read(buffer, 0, buffer.Length);
                Decoder decoder = Encoding.UTF8.GetDecoder();
                char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
                decoder.GetChars(buffer, 0, bytes, chars, 0);
                data.Append(chars);
                if (data.ToString().IndexOf("<EOF>") != -1)
                {
                    break;
                }

            }
            while (bytes != 0);
            Console.WriteLine(data.ToString());
            Console.ReadKey();
        }

    }
}

证书是自签名的,所以我必须与客户一起发送,但我不知道如何发送。即使我通过ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 禁用了验证,它也不起作用(我只是把它放在main 方法的第一行)。

【问题讨论】:

标签: c# ssl tcp tcpclient


【解决方案1】:

所以,我只是没有更改代码,我只是使用本指南在我的服务器上安装了证书:https://technet.microsoft.com/en-us/library/ff730672.aspx

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 2014-09-24
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多