CefSharp访问需要认证网页或接口(在Request的Headers中添加认证Token)

public class MyRequestHandler : DefaultRequestHandler
    {
        public static readonly string VersionNumberString = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
            Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
 
        public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            Uri url;
            if (Uri.TryCreate(request.Url, UriKind.Absolute, out url) == false)
            {
                return CefReturnValue.Cancel;
            }
            var headers = request.Headers; 
            headers["Authorization"] =Program.Token; //传递进去认证Token
            request.Headers = headers; 
            return CefReturnValue.Continue;
        }       
    }

然后在使用的时候,就可以获取到Header的内容了。

……
private ChromiumWebBrowser browser;
……
browser.RequestHandler = new MyRequestHandler();
……

  

你可能感兴趣的:(CefSharp访问需要认证网页或接口(在Request的Headers中添加认证Token))