需要身份验证时不调用GetAuthCredentials

时间:2020-05-06 07:17:57

标签: c# wpf openlayers cefsharp

我有一个包含ChromiumWebBrowser的控件(ECBControl)

<cefSharp:ChromiumWebBrowser x:Name="_browser" Grid.Column="0" Grid.ColumnSpan="2" AllowDrop="True"
  Address="{Binding ElementName=_this, Path=Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  FrameLoadStart="_browser_FrameLoadStart" FrameLoadEnd="_browser_FrameLoadEnd" 
  MouseWheel="_browser_MouseWheel"                                             
  RenderOptions.BitmapScalingMode="{Binding ElementName=_this, Path=BitmapScaling, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" PreviewKeyDown="_browser_OnPreviewKeyDown"></cefSharp:ChromiumWebBrowser>

包含ECBControl的对话框(ECBBrowser)注册一个地图处理程序

private void ECBMapDialog_Loaded(object sender, RoutedEventArgs e)
{
    this.ECBBrowser.Browser.RequestHandler = new ECBMapHandler(this);        
}

通过一些Javascript交互(在另一个SO-问题CefSharp Javascript registration and execution is not working in Release 79.1.36中进行了介绍),一个网站被多次调用以获取地图图像

Fiddler Log

这是我期望ECBMapHandler的GetAuthCredentials被调用的地方

public bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
    try
    {
        if (isProxy && ECBConfiguration.Instance.ProxyAuthentication)
        {
            var userName = Core.Applications.CoreApplication.ClientCredentials.GetUserName();
            var password = Core.Applications.CoreApplication.ClientCredentials.GetPassword();

            callback.Continue(userName, password);
            return true;
        }

        if (this.Provider.Credentials.TryGetValue(host, out var credentials))
        {
            callback.Continue(credentials.GetUserName(), credentials.GetPassword());
            return true;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    callback.Cancel();
    return false;
}

但是它从未被调用,因此该地图未在浏览器中显示。 我需要做些什么才能使其正常工作?

更新

我基于MinimalExample解决方案创建了一个示例。 您可以在https://github.com/tbremeyer/CefSharpSample.git上找到它。 此示例的所有操作都在MainWindows.xaml.cs中:

private async Task CallWebSite()
{
    if (_callsPerformed > _maxCalls) return;
    var executingAssembly = Assembly.GetExecutingAssembly();
    var resourcePath = "CefSharp.MinimalExample.Wpf" + ".web.index.html";

    if (executingAssembly.GetManifestResourceInfo(resourcePath) != null)
    {
        var resourceStream = executingAssembly.GetManifestResourceStream(resourcePath);
        var memoryStream = new MemoryStream();
        await resourceStream.CopyToAsync(memoryStream);
        resourceStream.Close();
        memoryStream.Position = 0;
        var fileText = await new StreamReader(memoryStream).ReadToEndAsync();
        Browser.LoadHtml(fileText, true);
    }

    var result = await Browser.EvaluateScriptAsync($"httpGet('http://httpbin.org/basic-auth/undefined/undefined?accept=json')");
    _callsPerformed++;
}

从资源文件加载html文件,然后将其加载到浏览器中。 html包含一个脚本,该脚本对作为参数传递的url执行get请求。该网址需要身份验证,您将获得401(在Fiddler中可以看到)。永远不会调用MinimalExampleHandler中的GetAuthCredentials方法。

0 个答案:

没有答案
相关问题