gpt4 book ai didi

.net - TrustAllCertificatesCallback 被忽略

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:30 25 4
gpt4 key购买 nike

给定一个 url 或服务器名称,我如何使用 powershell 或 .net 库下载 Web 服务器正在使用的(过期的)证书,然后将其保存到文件或将其导入我的证书存储区?

谢谢!

我已经取得了进步,我在这个问题上走了这么远:

static class Program
{
static void Main()
{
ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificatesCallback;
var tcpclient = new TcpClient("remote.example.com", 443);
var tcpstream = tcpclient.GetStream();
var sslstream = new SslStream(tcpstream);
sslstream.AuthenticateAsClient("remote.example.com");
X509Certificate rc = sslstream.RemoteCertificate;
Console.WriteLine(rc.ToString());
Console.ReadLine();
}

public static bool TrustAllCertificatesCallback(
object sender, X509Certificate cert,
X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
return true;
}
}

现在,当我运行这个程序时,我在 AuthenticateAsClient 行上得到一个 AuthenticationException,它说“根据验证程序,远程证书无效。”我在 return true; 上使用断点运行它,它从未调用过 TrustAllCertificatesCallback。我认为程序集存在权限或配置问题,有人知道如何解决吗?

最佳答案

我没有做过这样的事情,但看看这些例子我想我可以看出问题所在。试试这个代码:

static class Program
{
static void Main()
{
var tcpclient = new TcpClient("remote.example.com", 443);
var tcpstream = tcpclient.GetStream();
var sslstream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback (TrustAllCertificatesCallback));
sslstream.AuthenticateAsClient("remote.example.com");
X509Certificate rc = sslstream.RemoteCertificate;
Console.WriteLine(rc.ToString());
Console.ReadLine();
}

public static bool TrustAllCertificatesCallback(
object sender, X509Certificate cert,
X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
return true;
}
}

关于.net - TrustAllCertificatesCallback 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3312359/

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