Where to place RemoteCertificateValidationCallback?

后端 未结 3 1434
忘了有多久
忘了有多久 2020-12-20 05:59

I have the same problem as here: How to disable "Security Alert" window in Webbrowser control

I like the answer, but where am I going to place the Se

相关标签:
3条回答
  • 2020-12-20 06:49

    For someone looking to do this in powershell, you can use the following. it is important to not do {$true} for the handler as if called often it can result in a out of runspace error.

    $code = @"
    public class SSLHandler
    {
        public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
        {
    
            return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
        }
        
    }
    "@
    
    Add-Type -TypeDefinition $code
    #disable checks
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()
    #do the request
    try
    {
        invoke-WebRequest -Uri myurl -UseBasicParsing
    } catch {
        # do something
    } finally {
       #enable checks again
       [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    }
    

    if you still have some servers running powershell v2 you cannot use an anonymous function, this version will work:

    $code = @"
    public class SSLHandler
    {
        private static bool Callback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    
        public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
        {
    
            return new System.Net.Security.RemoteCertificateValidationCallback(Callback);
        }
        
    }
    "@
    
    0 讨论(0)
  • 2020-12-20 06:52

    Try this:

    private static bool ValidateRemoteCertificate(
      object sender,
      X509Certificate certificate,
      X509Chain chain,
      SslPolicyErrors policyErrors)
    {
        // Logic to determine the validity of the certificate
         // return boolean
    }
    
    
    // allows for validation of SSL conversations
                ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
                    ValidateRemoteCertificate
                );
    
    HtmlElementCollection ellements = webBrowser.Document.GetElementsByTagName("input");
    foreach (HtmlElement ellement in ellements)
    {
        if (ellement.OuterHtml == "<INPUT onclick=\"this.value = 'Submitted'\" value=\" Login \" type=submit>")
        {
            ellement.InvokeMember("click");
            this.DialogResult = DialogResult.OK;
            break;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 06:53

    You should put the following at any point before you show the web browser control / submit the page:

    ServicePointManager.ServerCertificateValidationCallback += 
        new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
    

    (This is exactly the same as the example answer in the linked question, but the callback method is anonymous so its a little more compact).

    0 讨论(0)
提交回复
热议问题