4

i'm trying to use proxy with Auth on cefsharp i tried this code and it's working with proxy without Auth only what should i do to set 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);
                    });

i found this link but i don't understand how to do it

https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-proxy-resolution please write some code i'm beginer.

Sameh Nabil
  • 73
  • 1
  • 5
  • The documentation is https://github.com/cefsharp/CefSharp/wiki/General-Usage#proxy-resolution I cannot help you with an example. Best of luck! – amaitland Feb 14 '17 at 11:34

2 Answers2

5

I'm back with a real answer this time.

What you need to do is to implement your own class using IRequestHandler and call the 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;

     }
}

The callback.Continue() will apply the credentials for you when called.

You then implement the handler to your browser instance in the code you allready have.

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);
});
Jomasdf
  • 258
  • 2
  • 13
  • Hello, i can't do this: 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; } } without implementing rest off IRequestHandler how do you fix this? – Samhakadas Jul 13 '17 at 22:25
  • Just remove the stuff you don't want. Here is my entire MyRequestHandler. Pastebin - https://pastebin.com/J6F7kwir – Jomasdf Jul 17 '17 at 01:38
  • 2
    I changed the base class to DefaultRequestHandler so you don’t have to add a lot of boilerplate code – Konstantin S. Feb 20 '19 at 16:51
  • Konstanin S. probably meant it that way: default `RequestHandler`. Because `DefaultRequestHandler` does not exist... in modern Cefsharp at least. – Kosmo零 Jul 25 '22 at 09:46
3

I use cefsharp version 65, just use this code:

CefSharpSettings.Proxy = new ProxyOptions(ip: "myipaddress", port: "myport", username: "myusername", password: "mypassword");

Put it before Cef.Initialize() and it works for me

slfan
  • 8,950
  • 115
  • 65
  • 78
congchien
  • 31
  • 1