TrustAllCertificatesCallback被忽略

Seg*_*ult 4 .net powershell ssl

给定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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

Jas*_*her 9

我没有做过这样的事情,但看看我认为可以看出错误的例子.试试这段代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在PoshCode上有一个[SSL Oblivious Web Client] [1],并且[Get-Cert] [2]实际上获得了证书而不是仅仅信任所有证书.[1]:http://poshcode.org/624 [2]:http://poshcode.org/69 (2认同)