【发布时间】: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 方法的第一行)。
【问题讨论】:
-
查看codeproject源代码:codeproject.com/Articles/1000189/…
-
那么你的意思是c++部分吗? :D 或者我如何安装证书?
-
对不起。 SSL 有很多代码项目,我选择了 c++ 而不是 c# 项目。试试这个:codeproject.com/script/Articles/ViewDownloads.aspx?aid=326574