wpf CefSharp 使用浏览器登录加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29612875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:04:41  来源:igfitidea点击:

CefSharp load a page with browser login

wpfbrowserloadcefsharp

提问by Alain BUFERNE

I need to ebed a web browser in a Wpf app, I tried with the one from the toolbox but get some issues and went to CefSharp.

我需要在 Wpf 应用程序中使用 Web 浏览器,我尝试使用工具箱中的浏览器,但遇到一些问题并转到 CefSharp。

public MainWindow()
{
 InitializeComponent();
 BrowserSettings settings = new BrowserSettings();
 Cef.Initialize(new CefSettings());           
CefSharp.Wpf.ChromiumWebBrowser webBrowser = new CefSharp.Wpf.ChromiumWebBrowser();
  licence_grid.Children.Add(webBrowser);
  webBrowser.Address = "http://myurlToLoad the page";
}

The problem is when I used a normal url the page load. But when I used the url I intend to use and whith which the user enter his user and password in a browser pop up (I mean not a pop up from the website) . I get an error with this page take yoo much time to load and nothing else. Can someone give me some tracks to follow... Thanks

问题是当我使用普通 url 加载页面时。但是当我使用我打算使用的 url 时,用户在浏览器中输入他的用户名和密码时会弹出(我的意思是不是从网站上弹出)。我收到一个错误,这个页面需要很长时间才能加载,没有别的。有人可以给我一些曲目吗...谢谢

采纳答案by jornh

It sounds like the popup you are referring to is in fact the site prompting for basicauthentication.

听起来您所指的弹出窗口实际上是提示进行basic身份验证的站点。

In that case you need to provide an IRequestHandler.GetAuthCredentialshandler.

在这种情况下,您需要提供一个IRequestHandler.GetAuthCredentials处理程序。

回答by shyam_

As the question & answer is very old and i would like to give the latest update on this solution, there is slight change as per original solution suggested.

由于问题和答案很旧,我想提供有关此解决方案的最新更新,因此根据建议的原始解决方案略有更改。

anybody consuming cefsharp need to implement the authentication dialog. and changes in method is

任何消费 cefsharp 的人都需要实现身份验证对话框。方法的变化是

 bool IRequestHandler.GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, 
        string host, int port, string realm, string scheme, IAuthCallback callback)
    {
        //NOTE: If you do not wish to implement this method returning false is the default behaviour
        // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.

        // shyam - original implemenation.
        //callback.Dispose();
        //return false;

        bool handled = false;

        // Instantiate the dialog box
        AuthDialog dlg = new AuthDialog(host); // create new dialog with username and password field.

        // Open the dialog box modally 
        dlg.ShowDialog();

        if (dlg.DialogResult == System.Windows.Forms.DialogResult.OK)
        {
            // The user did not cancel out of the dialog. Retrieve the username and password.
            callback.Continue(dlg.UserName,dlg.Password);
            handled = true;
        }

        return handled;
    }