1. 程式人生 > >CefSharp訪問需要認證網頁或接口(在Request的Headers中添加認證Token)

CefSharp訪問需要認證網頁或接口(在Request的Headers中添加認證Token)

內容 and try only abs ber private uri continue

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)