gpt4 book ai didi

c# - 为什么每次都遇到RemoteCertificateNameMismatch?

转载 作者:太空狗 更新时间:2023-10-30 00:19:07 26 4
gpt4 key购买 nike

考虑以下完整程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace RemoteCertNameMismatchDiagnosis
{
class Program
{
private static bool AcceptAllCertificates(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine(sslPolicyErrors.ToString());
return true;
}

static void Main(string[] args)
{
TcpClient client;
SslStream sslStream;

bool acceptAnyCert = false;

client = new TcpClient("google.com", 443);
if (acceptAnyCert)
sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(AcceptAllCertificates), null);
else
sslStream = new SslStream(client.GetStream(), false);

try
{
sslStream.AuthenticateAsClient("test client");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadLine();
}
}
}

它报告这个异常

System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

每一次。通过在第 26 行将 acceptAnyCert 更改为 true,我可以让它输出这个

RemoteCertificateNameMismatch

,让我相信它对证书上的名字不满意。

无论我在第 28 行指向 google.com、amazon.com 还是其他任何地方,这种行为都会持续存在。我不认为 google、microsoft 和 amazon 的证书都有缺陷。我做错了什么?

最佳答案

您需要将 "google.com" 传递给 AuthenticateAsClient - 它需要服务器名称作为参数,而不是您的客户端名称。

关于c# - 为什么每次都遇到RemoteCertificateNameMismatch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25001659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com