.net TrustAllCertificatesCallback被忽略

.net TrustAllCertificatesCallback被忽略,.net,ssl,powershell,.net,Ssl,Powershell,给定url或服务器名称,如何使用powershell或.net库下载web服务器正在使用的(过期)证书,然后将其保存到文件或导入到我的证书存储中 谢谢 我已经取得了进步,在这个问题上我已经走了这么远: static class Program { static void Main() { ServicePointManager.ServerCertificateValidationCallback = TrustAllCertifi

给定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;
        }
    }

现在,当我运行这个程序时,我在
authenticatesclient
行上得到一个AuthenticationException,它说“根据验证过程,远程证书无效。”我在
返回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;
    }
}

PoshCode上有一个[SSL不经意Web客户端][1],还有一个[Get Cert][2],它实际上获得了证书,而不仅仅是信任所有证书。[1]: [2]: