Make a web browser that only allows certain web sites in C#


The WebBrowser control is a full-featured web browser in a control that you can place on your form. You can use its Navigate method to make the browser go to a specific URL. You can also catch its Navigating event and decide whether to allow the browser to visit a URL.

In this example, you can enter a URL in the TextBox and click Go to make the browser try to visit that site. Here’s how the program navigates to the site you enter:

// Try to navigate to the entered URL.
private void btnGo_Click(object sender, EventArgs e)

This code simply calls the WebBrowser‘s Navigate method.

The program uses the following code to allow the browser to only go to locations in the web site.

// Verify that this is an allowed web site.
private void webBrowser1_Navigating(object sender,
    WebBrowserNavigatingEventArgs e)
    if (e.Url.Host != "")
        e.Cancel = true;
        MessageBox.Show("This web site is prohibited",
            "Prohibited", MessageBoxButtons.OK, 

The Navigating event handler examines the URL’s host. If the host isn’t, the code cancels the navigation and display an error message.

To test the program, you can enter another web site in the TextBox (such as Google or Yahoo) and click Go. You can also click the Twitter icon (on the lower left in the picture above) to try to go to

Download Example   Follow me on Twitter   RSS feed

This entry was posted in controls, internet, web and tagged , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Make a web browser that only allows certain web sites in C#

  1. Viviscal Reviews

    BLOG.CSHARPHELPER.COM: Make a web browser that only allows certain web sites in C#

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.