防止WebBrowser自动完成密码弹出

时间:2017-12-14 18:33:46

标签: c# wpf

我在WPF中有一个WebBrowser,我在成功登录网站后不断获得此弹出窗口

enter image description here

我尝试使用隐藏脚本和警报拦截器来抑制此操作但没有成功。

有没有办法在代码中禁止弹出窗口?

代码:

        <WebBrowser x:Name="wpfBrowser"
                    Height="450"
                    MinHeight="450"
                    Width="825"
                    MinWidth="825"
                    view:WebBrowserBehaviors.BindableSource="{Binding LoginViewModel.URL}" 
                    Navigated="wpfBrowser_Navigated"
                    Loaded="wpfBrowser_Loaded"
                    LoadCompleted="wpfBrowser_LoadCompleted" KeyUp="wpfBrowser_KeyUp">                
        </WebBrowser>


    public void HideScriptErrors(WebBrowser wb, bool hide)
    {
        var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
        if (fiComWebBrowser == null) return;
        var objComWebBrowser = fiComWebBrowser.GetValue(wb);
        if (objComWebBrowser == null)
        {
            wb.Loaded += (o, s) => HideScriptErrors(wb, hide); //In case we are to early
            return;
        }
        objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
    }

    private void InjectAlertBlocker()
    {
        mshtml.HTMLDocument document = (mshtml.HTMLDocument)wpfBrowser.Document;
        mshtml.IHTMLElement head = document.getElementsByTagName("head").Cast<mshtml.IHTMLElement>().First();
        var script = (mshtml.IHTMLScriptElement)document.createElement("script");
        string alertBlocker = "window.alert = function () { }";
        ((mshtml.HTMLHeadElement)head).appendChild((mshtml.IHTMLDOMNode)script);
    }

    private void wpfBrowser_Loaded(object sender, RoutedEventArgs e)
    {
        HideScriptErrors(wpfBrowser, true);
    }

    private void wpfBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        try
        {
            InjectAlertBlocker();
        }
        catch (Exception ex)
        {
            NLog.Log(LogLevel.Error, ex, "Logs");
        }
    }

1 个答案:

答案 0 :(得分:1)

此弹出窗口不是来自JavaScript ......来自IE。

如果您可以从应用中访问注册表,则可以尝试此操作:

https://support.microsoft.com/en-us/help/229940/how-to-disable-internet-explorer-password-caching

但是:这也取消了你的app以外的普通IE浏览器的弹出窗口!!!