如何绕过SSL错误CefSharp WinForms [英] How to bypass SSL error CefSharp WinForms

查看:821
本文介绍了如何绕过SSL错误CefSharp WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CefSharp.WinForms 开发应用程序。
发生任何SSL证书错误时,它将不会显示网页。
谁能告诉我如何绕过SSL证书错误并显示网页。

I'm using CefSharp.WinForms for developing an application. When there is any SSL Certificate Error occurs it won't display web page. Can any one tell me how can i bypass SSL Certificate Error and display the web page.

推荐答案

选项1(首选)



实施 IRequestHandler.OnCertificateError -将为每个无效证书调用此方法。如果仅希望覆盖 IRequestHandler 的一些方法,则可以从 RequestHandler override 您感兴趣的方法,在本例中为 OnCertificateError

Option 1 (Preferred)

Implement IRequestHandler.OnCertificateError - this method will be called for every invalid certificate. If you only wish to override a few methods of IRequestHandler then you can inherit from RequestHandler and override the methods you are interested in specifically, in this case OnCertificateError

//Make sure you assign your RequestHandler instance to the `ChromiumWebBrowser`
browser.RequestHandler = new ExampleRequestHandler();

public class ExampleRequestHandler : RequestHandler
{
    protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
    {
        //NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.

        //Example #1
        //Return true and call IRequestCallback.Continue() at a later time to continue or cancel the request.
        //In this instance we'll use a Task, typically you'd invoke a call to the UI Thread and display a Dialog to the user
        Task.Run(() =>
        {
            //NOTE: When executing the callback in an async fashion need to check to see if it's disposed
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    //We'll allow the expired certificate from badssl.com
                    if (requestUrl.ToLower().Contains("https://expired.badssl.com/"))
                    {
                        callback.Continue(true);
                    }
                    else
                    {
                        callback.Continue(false);
                    }
                }
            }
        });

        return true;

        //Example #2
        //Execute the callback and return true to immediately allow the invalid certificate
        //callback.Continue(true); //Callback will Dispose it's self once exeucted
        //return true;

        //Example #3
        //Return false for the default behaviour (cancel request immediately)
        //callback.Dispose(); //Dispose of callback
        //return false;
    }
}






选项2



设置 CefSettings.IgnoreCertificateErrors

var settings = new CefSettings()
{
    IgnoreCertificateErrors = true
};

Cef.Initialize(settings);




  • WPF CefSettings示例

  • < a href = https://github.com/cefsharp/CefSharp.MinimalExample/blob/cefsharp/75/CefSharp.MinimalExample.WinForms/Program.cs#L24 rel = noreferrer> WinForms CefSettings示例

    • WPF CefSettings Example
    • WinForms CefSettings Example
    • 这篇关于如何绕过SSL错误CefSharp WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆