如何将CefSharp浏览器限制为给定域 [英] How to restrict CefSharp Browser to given domain

查看:102
本文介绍了如何将CefSharp浏览器限制为给定域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止我的应用程序显示来自某个域(例如example.com)以外的网页。

I would like to prevent my application to display web pages other than from a certain domain (i.e. example.com).

我最初的想法是检查请求 OnBeforeBrowse 事件处理程序中的URL。

My initial idea was to check the request URL in the OnBeforeBrowse event handler.

public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, bool isRedirect)
{
  return !IsPageAllowed(request.Url);
}

这看起来是可行的,除了允许页面上的所有嵌入式资源

例如,我的网页上嵌入了YouTube视频,video元素触发了两个附加请求。
如果我取消这些请求,则根本不会渲染视频。

这是我的请求的简单日志输出:

This looks doable, except for the fact that all embedded resources on a allowed page are also triggering this event.
For example, i have a YouTube video embedded on my page there are two additional requests triggered by the video element. If i cancel those requests, video is not rendered at all.
Here is a simple log output from my request:

15:09:22.5809442 - OnBeforeBrowse: http://example.com/, TransitionType=LinkClicked, isRedirect=False
15:09:22.6705460 - OnBeforeBrowse: http://www.youtube.com/embed/XYZ, TransitionType=LinkClicked, isRedirect=False
15:09:22.7715542 - OnBeforeBrowse: http://www.youtube.com/embed/XYZ, TransitionType=LinkClicked, isRedirect=True
15:09:25.1232542 - OnBeforeBrowse: http://not-allowed-domain.com TransitionType=LinkClicked, isRedirect=False

此外,如果我尝试通过单击外部链接(已禁用限制检查)从允许的页面导航离开,则会收到新事件,并且 isRedirect 标志设置为错误,这很令人困惑。

Further more, if i try to navigate away from the allowed page by clicking on a external link (with disabled restriction check), i get new event and isRedirect flag is set to False, which is quite confusing.

谢谢

推荐答案

项目我已经实现了以下解决方案:

In my project I've implemented the following solution:


  1. 必须准备受信任的URL或/和域的列表:



    HashSet whiteList = new HashSet() {
        "http://example.com/",
        "http://www.youtube.com/embed/",
        ...
    }


  • 过滤和阻止-在 CefRenderProcessHandler.OnBeforeNavigation()

  • 这篇关于如何将CefSharp浏览器限制为给定域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆