簡體   English   中英

驗證SSL證書

[英]Validate SSL Certificate

我正在嘗試通過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; };禁用驗證ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ,它不起作用(我只是將其放在main方法的第一行中)。

因此,我只是沒有更改代碼,而是使用以下指南在服務器上安裝了證書: https : //technet.microsoft.com/zh-cn/library/ff730672.aspx

暫無
暫無

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

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