How to disable “Security Alert” window in Webbrowser control

旧巷老猫 提交于 2019-12-17 09:46:12

问题


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:

I have to find this window by title and send it Alt+Y to press 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?


回答1:


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.




回答2:


Ok, article is up on code project - see http://www.codeproject.com/KB/shell/WebBrowserControlDialogs.aspx Hopefully this helps.




回答3:


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.

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.




回答4:


Here,we go with the solution: I run it on the Browser_Navigated event as the underlying activeX component is null until then.

Ref:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4f686de1-8884-4a8d-8ec5-ae4eff8ce6db/new-wpf-webbrowser-how-do-i-suppress-script-errors?forum=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 });

    }



回答5:


When I set the WebBrowser.ScriptErrorsSuppressed property to false, I do not get these popups anymore




回答6:


I see this solution.it work for me.It very easy way.



来源:https://stackoverflow.com/questions/178578/how-to-disable-security-alert-window-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!