简体   繁体   中英

Validate SSL Certificate

I am trying to connect my c# client to a node.js server via tls/ssl. The Server is working and I have created my certs. But If I want to connect via TcpClient to the Server, it says, that the Certificate is invalid. Could you make your own validation for that cert? Do you guys have any ideas?

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

    }
}

The Certificate is self signed so I have to send it with the client, but I dont know how. Even if I disable the validation via ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; , it does not work (I just put it in the first line of the main method).

因此,我只是没有更改代码,而是使用以下指南在服务器上安装了证书: https : //technet.microsoft.com/zh-cn/library/ff730672.aspx

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