我遇到与此问题相同的问题:How to disable "Security Alert" window in Webbrowser control
我喜欢这个答案,但是我要在哪里放置ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
?
在我使用以下代码提交学校网络的登录页面后,我收到“无效认证”消息:
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 :(得分:11)
在显示网络浏览器控件/提交页面之前,您应该将以下内容放在任何点:
ServicePointManager.ServerCertificateValidationCallback +=
new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
(这与链接问题中的示例答案完全相同,但回调方法是匿名的,因此它更紧凑)。
答案 1 :(得分:2)
试试这个:
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;
}
}
答案 2 :(得分:0)
对于希望在Powershell中执行此操作的人,请使用以下命令。重要的是,对于处理程序,不要执行{$ true} ,因为经常调用它可能会导致运行空间不足错误。
$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
}
如果仍然有一些服务器在运行Powershell v2,则不能使用匿名功能,此版本将起作用:
$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);
}
}
"@