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

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

问题描述

我正在使用 CefSharp.WinForms 来开发应用程序.当出现任何 SSL 证书错误时,将不会显示网页.

I'm using CefSharp.WinForms to develop an application. When any SSL certificate error occurs, it won't display the web page.

如何绕过 SSL 证书错误并显示网页?

How can I bypass SSL certificate error and display the web page?

推荐答案

选项1(首选)

实现 IRequestHandler.OnCertificateError - 这个方法将是要求每个无效证书.如果您只想覆盖 IRequestHandler 的一些方法,那么您可以从 RequestHandleroverride 您特别感兴趣的方法,在本例中为 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

设置忽略证书错误命令行参数


Option 2

Set ignore-certificate-errors command line arg

var settings = new CefSettings();
settings.CefCommandLineArgs.Add("ignore-certificate-errors");

Cef.Initialize(settings);

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