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

CefSharp load a page with browser login

我需要在 Wpf 应用程序中嵌入网络浏览器,我尝试使用工具箱中的浏览器,但遇到了一些问题并转至 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";
}

问题是当我使用正常 url 页面加载时。 但是当我使用 url 时,我打算使用它,用户在弹出的浏览器中输入他的用户名和密码(我的意思是不是从网站弹出)。我收到此页面的错误,加载时间太长,没有其他问题。 有人可以给我一些跟踪... 谢谢

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

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

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

任何使用 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;
    }