2017-02-14 234 views
2

我想使用代理与身份验证在cefsharp 我试过这段代码,它与代理没有身份验证只有 我应该怎么做来设置身份验证。cefsharp代理身份验证

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); 
        }); 

,我发现这个链接,但我不明白怎么做了

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

+0

该文档是https://github.com/cefsharp/CefSharp/wiki/General-Usage#proxy-resolution我不能帮你一个例子。祝你好运! – amaitland

回答

3

这次我回来了一个真正的答案。

你需要做的是使用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; 

    } 
} 

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); 
}); 
+0

你好,我不能做到这一点: 公共类MyRequestHandler:IRequestHandler { 布尔IRequestHandler.GetAuthCredentials(IWebBrowser browserControl,IBrowser浏览器,IFrame的框架,布尔isProxy,串主机,诠释端口,串境界,串方案,IAuthCallback回调) { if(isProxy == true) { callback.Continue(“Username”,“Password”); 返回true; } return false; } } 没有执行休息IRequestHandler 你如何解决这个问题? – Samhakadas

+0

只要删除你不想要的东西。这是我的整个MyRequestHandler。 Pastebin - https://pastebin.com/J6F7kwir – Jomasdf