【问题标题】:Ignoring web browser certificate errors in a webbrowser忽略网络浏览器中的网络浏览器证书错误
【发布时间】:2014-09-23 18:01:00
【问题描述】:

我正在尝试忽略 Web 浏览器中出现的 Web 浏览器证书错误,如下所示。

我正在尝试使用this 解决方案(另请参见下面的代码),但它对我不起作用,仍然显示证书错误对话框。我的猜测是:一定要我得到that 对话框而不是像this 这样的页面吗?

C# 代码(摘自文章):

[Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface UCOMIServiceProvider
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int QueryService(
        [In] ref Guid guidService,
        [In] ref Guid riid,
        [Out] out IntPtr ppvObject);
}
[ComImport()]
[ComVisible(true)]
[Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWindowForBindingUI
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int GetWindow(
        [In] ref Guid rguidReason,
        [In, Out] ref IntPtr phwnd);
}
[ComImport()]
[ComVisible(true)]
[Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IHttpSecurity
{
    //derived from IWindowForBindingUI
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int GetWindow(
        [In] ref Guid rguidReason,
        [In, Out] ref IntPtr phwnd);
    [PreserveSig]
    int OnSecurityProblem(
        [In, MarshalAs(UnmanagedType.U4)] uint dwProblem);
}
public class MyWebBrowser : WebBrowser
{
    public static Guid IID_IHttpSecurity
        = new Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b");
    public static Guid IID_IWindowForBindingUI
        = new Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b");
    public const int S_OK = 0;
    public const int S_FALSE = 1;
    public const int E_NOINTERFACE = unchecked((int)0x80004002);
    public const int RPC_E_RETRY = unchecked((int)0x80010109);
    protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
    {
        return new MyWebBrowserSite(this);
    }
    class MyWebBrowserSite : WebBrowserSite,
        UCOMIServiceProvider,
        IHttpSecurity,
        IWindowForBindingUI
    {
        private MyWebBrowser myWebBrowser;
        public MyWebBrowserSite(MyWebBrowser myWebBrowser)
            :base(myWebBrowser)
        {
            this.myWebBrowser = myWebBrowser;
        }
        public int QueryService(ref Guid guidService
            , ref Guid riid
            , out IntPtr ppvObject)
        {
            if (riid ==IID_IHttpSecurity)
            {
                ppvObject= Marshal.GetComInterfaceForObject(this
                    , typeof(IHttpSecurity));
                return S_OK;
            }
            if (riid == IID_IWindowForBindingUI)
            {
                ppvObject = Marshal.GetComInterfaceForObject(this
                    , typeof(IWindowForBindingUI));
                return S_OK;
            }
            ppvObject = IntPtr.Zero;
            return E_NOINTERFACE;
        }
        public int GetWindow(ref Guid rguidReason
            , ref IntPtr phwnd)
        {
            if (rguidReason == IID_IHttpSecurity
                || rguidReason == IID_IWindowForBindingUI)
            {
                phwnd = myWebBrowser.Handle;
                return S_OK;
            }
            else
            {
                phwnd = IntPtr.Zero;
                return S_FALSE;
            }
        }
        public int OnSecurityProblem(uint dwProblem)
        {
            //ignore errors
            //undocumented return code, does not work on IE6
            return S_OK;
        }
    }
}

我是这样使用的:

MyWebBrowser web = new MyWebBrowser();
            web.Navigate("https://...");
            web.Dock = DockStyle.Fill;
            this.Controls.Add(web);

我该如何解决这个问题?

【问题讨论】:

  • "...但它对我不起作用"。 - 这是什么意思?
  • 是否意味着证书没有被忽略?如果不清楚,我会编辑
  • ppvObject= Marshal.GetComInterfaceForObject(this , typeof(IHttpSecurity)) 上设置断点。它会被触发吗?
  • 这个重复的问题甚至没有被接受的答案。不确定作者是否解决了您的问题。

标签: c# winforms ssl browser webbrowser-control


【解决方案1】:

如果您想抑制这些消息,请尝试设置 WebBrowser 属性 ScriptErrorsSuppressed

webBrowser.ScriptErrorsSuppressed = true

这应该可以解决您的问题

您还可以在这里看到另一种抑制这些消息的方法,并进行更具体的处理

http://msdn.microsoft.com/en-GB/library/system.windows.forms.webbrowser.scripterrorssuppressed.aspx

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多