3

I'm trying to Ignore web browser certificate errors in a webbrowser which appear in the web browser like below.

enter image description here

I'm trying to use this solution (see below code also) but it didn't worked for me, the certificate error dialog box still is displayed. My guess is: Does have to do I'm getting that dialog box rather than a pagine like this?

C# Code (take from article):

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

And I'm using like this:

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

How do I fix this?

noseratio
  • 59,932
  • 34
  • 208
  • 486
Jack
  • 16,276
  • 55
  • 159
  • 284
  • "...but it didn't worked for me". - what does that mean? – jww Jul 31 '14 at 17:12
  • does means that the certificate isn't ignored? I'll edit if it isn't clear – Jack Jul 31 '14 at 17:13
  • Put a breakpoint on `ppvObject= Marshal.GetComInterfaceForObject(this , typeof(IHttpSecurity))`. Does it ever get triggered? – noseratio Aug 01 '14 at 09:07
  • possible duplicate of [WebBrowser control - ignore SSL errors](http://stackoverflow.com/questions/6933254/webbrowser-control-ignore-ssl-errors) – KidCode Aug 01 '14 at 09:34
  • This duplicate question doesn't even an accepted answer. Not sure of the author solved your problem. – Jack Aug 05 '14 at 18:55

1 Answers1

0

If you want to suppress those messages, try setting WebBrowser property ScriptErrorsSuppressed

webBrowser.ScriptErrorsSuppressed = true

This should fix your problem

Also you can see here another way to suppress those messages, with more specific handling

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

Guy Levin
  • 1,230
  • 1
  • 10
  • 22