需要 cefsharp 代理身份验证

Sam*_*bil 4 c# proxy chromium-embedded cefsharp

我正在尝试在 cefsharp 上使用具有 Auth 的代理,我尝试了此代码,它在没有 Auth 的情况下与代理一起使用,我应该如何设置 Auth 。

Cef.UIThreadTaskFactory.StartNew(delegate
                    {
                        string ip = "IP";
                        string port = "PORT";
                        var rc = chrome.GetBrowser().GetHost().RequestContext;
                        var dict = new Dictionary<string, object>();
                        dict.Add("mode", "fixed_servers");
                        dict.Add("server", "" + ip + ":" + port + "");
                        string error;
                        bool success = rc.SetPreference("proxy", dict, out error);
                    });
Run Code Online (Sandbox Code Playgroud)

我找到了这个链接,但我不明白该怎么做

https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-proxy-resolution 请写一些代码,我是初学者。

Jom*_*sdf 5

这次我带着真实的答案回来了。

您需要做的是使用 IRequestHandler 实现您自己的类并调用 GetAuthCredentials()。

public class MyRequestHandler : IRequestHandler
{

    bool IRequestHandler.GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
    {

        if (isProxy == true)
        {             
                callback.Continue("Username", "Password");

                return true;
        }

        return false;

     }
}
Run Code Online (Sandbox Code Playgroud)

调用时,callback.Continue() 将为您应用凭据。

然后,您可以在已有的代码中将处理程序实现到浏览器实例。

Cef.UIThreadTaskFactory.StartNew(delegate
{

      chrome.RequestHandler = new MyRequestHandler();

      string ip = "IP";
      string port = "PORT";
      var rc = chrome.GetBrowser().GetHost().RequestContext;
      var dict = new Dictionary<string, object>();
      dict.Add("mode", "fixed_servers");
      dict.Add("server", "" + ip + ":" + port + "");
      string error;
      bool success = rc.SetPreference("proxy", dict, out error);
});
Run Code Online (Sandbox Code Playgroud)

  • 我将基类更改为 DefaultRequestHandler,这样您就不必添加大量样板代码 (2认同)