如何防止用户浏览 WebBrowser 控件中的某些网站? [英] How can I prevent a user from browsing to certain web sites in a WebBrowser control?

查看:22
本文介绍了如何防止用户浏览 WebBrowser 控件中的某些网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序中嵌入了一个简单的网络浏览器,它允许用户搜索维基百科,我试图阻止用户浏览维基百科以外的其他网站(例如,通过单击引用的外部链接).现在,我正在使用这个简单的代码:

I have a simple web browser embedded in an application that allows a user to search Wikipedia, and I'm trying to prevent the user from browsing to other websites outside wikipedia (e.g. by clicking an external link the references). Right now, I'm using this simple code:

this.textBoxAddress.Text = this.webBrowser.Url.ToString();
if (this.webBrowser.Url.Host != "en.wikipedia.org")
{
    this.webBrowser.Visible = false;
    this.labelErrorMessage.Visible = true;
}
else
{
    this.webBrowser.Visible = true;
    this.labelErrorMessage.Visible = false;
}

此代码有效,只是页面在运行时已加载.如果我将此代码放入 _Navigating 事件以尝试在页面加载之前 抢先停止用户,则会失败,因为 Web 浏览器控件未更新 Url 属性,直到页面加载完毕.

This code works, except the page is already loaded when it's run. If I put this code into the _Navigating event to try to preemptively stop the user before the page is loaded, it fails because the web browser control doesn't update the Url property until after the page is loaded.

我不喜欢这段代码的原因是因为页面在调用时已经加载,所以该页面中的任何错误(例如显示弹出窗口的脚本错误)都会显示,即使网页没有显示.

The reason I don't like this code is because the page has already loaded when it's called, so any errors in that page (e.g. script errors, which display a popup) show, even though the web page does not.

我读过一些资料,说出于各种原因不使用 WebBrowser 控件,但在这种情况下,安全性不是主要问题.我也不需要担心最终用户做什么,因为这个应用程序是一个内部应用程序,实际上只有一个用户,他不会绕过安全性.

I've read sources that say not to use the WebBrowser control, for various reasons, but in this case, security isn't a major issue. I also don't need to worry about the end-user doing something, since this application is an in-house application that has literally only one user, and he won't bypass the security.

在 IE 的设置中禁用所有其他页面也不是解决方案,因为该用户使用 IE 进行其他冲浪,并且该组织的内部网站也在 IE 上运行(并且这些地址发生变化,因此简单地允许所有这些页面将难以预料).

Disabling all other pages in IE's settings is not a solution either, because this user utilises IE for other surfing, and the intranet sites of this organisation run on IE as well (and those addresses change, so simply allowing all of them would be difficult to anticipate).

推荐答案

订阅 WebBrowser.Navigating 事件.检查 WebBrowserNavigatingEventArgs.Url 属性并设置 WebBrowserNavigatingEventArgs.Cancel=true 如果它不是维基百科.

Subscribe to the WebBrowser.Navigating event. Check the WebBrowserNavigatingEventArgs.Url property and set WebBrowserNavigatingEventArgs.Cancel=true if it's not wikipedia.

这篇关于如何防止用户浏览 WebBrowser 控件中的某些网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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