CefSharp加载带有浏览器登录的页面

5

我需要在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";
}

问题在于当我使用普通的URL时,页面可以加载。但是,当我使用我想要使用的URL并且用户在浏览器弹出窗口中输入用户名和密码时(我的意思是不是来自网站的弹出窗口),我得到一个错误,该页面需要太长时间才能加载,什么也没有。有人能给我一些指导吗...谢谢


1
你看到了什么错误?此外,有一些示例项目可以帮助您入门,请参见https://github.com/cefsharp/CefSharp.MinimalExample尝试使用示例加载您的网页,看看是否会出现类似的错误。如果您包括CefSharp的版本作为一般惯例,这也是有帮助的。 - amaitland
2个回答

5

听起来你提到的弹出窗口实际上是网站提示进行基本身份验证。

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


是的,我没有找到合适的词...所以你的意思是我需要实现一个处理程序,并自己创建提示窗口,获取用户和密码,并将这些值放入处理程序中传递的字符串参数中... - Alain BUFERNE
1
基本上,提供用户自己的对话框来捕获用户名和密码,然后设置两个 ref 参数并返回 true。 - amaitland
好的,非常感谢。我来自于 MS ActiveX,所以我需要自己发掘。我已经发现相关文档不是很好。 - Alain BUFERNE
你实现了GetAuthCredentials函数吗?它能正常工作吗? - shyam_
事实上,我改变了主意,转而使用Electron来开发我的应用程序。 - Alain BUFERNE
我实现了GetAuthCredentials处理程序,在其中放置了一个模态窗口以接收用户和密码,但由于出现以下错误,它无法正常工作:System.InvalidOperationException:“调用线程无法访问此对象,因为不同的线程拥有它。” 这是处理程序中的代码: AuthBox dlg = new AuthBox(); dlg.Owner = _parent; dlg.ShowDialog();你知道发生了什么吗? - Cami Rodriguez

5

由于这个问题和回答非常古老,我想提供最新的解决方案更新,根据最初建议的方案有些变化。

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

1
AuthDialog类在哪里?Visual Studio找不到它。 - We are Borg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接