开发者

Where to place RemoteCertificateValidationCallback?

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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜