如何禁用“安全警报” Webbrowser 控件中的窗口

发布于 2024-07-07 23:00:11 字数 463 浏览 3 评论 0原文

我正在使用 Webbrowser 控件使用“不受信任的证书”登录 HTTPS 站点。 但我收到关于不受信任证书的弹出式标准窗口“安全警报”:

安全警报窗口

我必须按标题找到此窗口并发送Alt+Y以按

int iHandle = NativeWin32.FindWindow(null, "Security Alert");
NativeWin32.SetForegroundWindow(iHandle);
System.Windows.Forms.SendKeys.Send("Y%");

但用户可以看到此窗口闪烁。

如何忽略此警报?
或者在 Webbrowser 控件中禁用此“不受信任的证书”检查?

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:

Security Alert window

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林很绿却致人迷途 2024-07-14 23:00:12

这应该可以做到:

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

显然,盲目地允许证书是一种安全风险。 当心。

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.

半山落雨半山空 2024-07-14 23:00:12

如果证书不是来自受信任的证书颁发机构(提示中的第一点),那么您可以在相关 PC 上的受信任的根证书颁发机构下安装证书。

您可以在“查看证书”下执行此操作。

在某些方面,这可能是一个更简单的解决方案,因为它不需要任何接受任何和所有证书的代码更改。 但是,它确实要求在使用应用程序的任何地方安装证书。

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.

雪若未夕 2024-07-14 23:00:12

在这里,我们提出解决方案:
我在 Browser_Naviged 事件上运行它,因为在此之前底层 ActiveX 组件为空。

参考: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 });

    }

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

    }
很糊涂小朋友 2024-07-14 23:00:12

当我将 WebBrowser.ScriptErrorsSuppressed 属性设置为 false 时,我不再收到这些弹出窗口

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文