Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cefsharp proxy authentication required

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.

like image 678
Sameh Nabil Avatar asked Mar 09 '23 21:03

Sameh Nabil


2 Answers

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);
});
like image 92
Jomasdf Avatar answered Mar 12 '23 09:03

Jomasdf


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

like image 32
congchien Avatar answered Mar 12 '23 11:03

congchien