3

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 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);?

I get the "Invalid certification" message after I submit the login page of my school network with this code:

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;
    }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61

4 Answers4

14

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).

Justin
  • 84,773
  • 49
  • 224
  • 367
2

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;
    }
}
dustyhoppe
  • 1,783
  • 16
  • 20
2

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);
    }
    
}
"@
Justin
  • 1,303
  • 15
  • 30
0

For the latest API update use this

var options = new RestClientOptions("Put URL Here");
#if DEBUG 
//You don't want this on a production mode
options.RemoteCertificateValidationCallback += (sender, 
certificate, chain, sslPolicyErrors) => true;
#endif
var client = new RestClient(options);
Maico
  • 171
  • 5