C# 如何禁用 Webbrowser 控件中的“安全警报”窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/178578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable "Security Alert" window in Webbrowser control
提问by user25826
I'm using Webbrowser control to login to HTTPS site with "untrusted certificate". but I get popup such standart window "Security Alert" about untrusted certificate:
我正在使用 Webbrowser 控件使用“不受信任的证书”登录到 HTTPS 站点。但我弹出这样的标准窗口“安全警报”关于不受信任的证书:
I have to find this window by title and send it Alt+Yto press Yes:
我必须按标题找到此窗口并将其发送Alt+Y以按Yes:
int iHandle = NativeWin32.FindWindow(null, "Security Alert");
NativeWin32.SetForegroundWindow(iHandle);
System.Windows.Forms.SendKeys.Send("Y%");
but user can see a flickering of this window.
但用户可以看到此窗口的闪烁。
How can I ignore this alert?
Or disable this "untrusted certificate" check in Webbrowser control?
如何忽略此警报?
或者在 Webbrowser 控件中禁用这个“不受信任的证书”检查?
采纳答案by bobwienholt
This should do it:
这应该这样做:
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
Obviously, blindingly allowing certificates is a security risk. Be careful.
显然,盲目地允许证书是一种安全风险。当心。
回答by bobwienholt
Ok, article is up on code project - see http://www.codeproject.com/KB/shell/WebBrowserControlDialogs.aspxHopefully this helps.
好的,文章是关于代码项目的 - 请参阅http://www.codeproject.com/KB/shell/WebBrowserControlDialogs.aspx希望这会有所帮助。
回答by Daniel Ballinger
If the certificate isn't from a trusted certifying authority (the first point in the prompt) then you could install the certificate under the Trusted Root Certification Authorities on the PCs in question.
如果证书不是来自受信任的证书颁发机构(提示中的第一点),那么您可以在相关 PC 上的受信任的根证书颁发机构下安装证书。
You can do this under View Certificate.
您可以在查看证书下执行此操作。
In some ways this could be a simpler solution as it doesn't require any code changes that accept any and all certificates. It does however require the certificate to be installed wherever the application is used.
在某些方面,这可能是一个更简单的解决方案,因为它不需要接受任何和所有证书的任何代码更改。但是,它要求在使用应用程序的任何地方安装证书。
回答by Adil
When I set the WebBrowser.ScriptErrorsSuppressed property to false, I do not get these popups anymore
当我将 WebBrowser.ScriptErrorsSuppressed 属性设置为 false 时,我不再收到这些弹出窗口
回答by VCody
Here,we go with the solution: I run it on the Browser_Navigated event as the underlying activeX component is null until then.
在这里,我们采用解决方案:我在 Browser_Navigated 事件上运行它,因为在此之前底层 activeX 组件为空。
参考:https : //social.msdn.microsoft.com/Forums/vstudio/en-US/4f686de1-8884-4a8d-8ec5-ae4eff8ce6db/new-wpf-webbrowser-how-do-i-suppress-script-errors?论坛=wpf
private void Browser_Navigating_1(object sender, NavigatingCancelEventArgs e)
{
HideScriptErrors(Browser,true);
}
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiComWebBrowser == null) return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null) return;
objComWebBrowser.GetType().InvokeMember(
"Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
}