给定一个 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;
}
}
我是一名优秀的程序员,十分优秀!