ASP.NET – Block IP Addresses from Your Site


Today I want to present a way how to Block an IP from your website. So that these blocked IP address can not open your site. I work on this on my recent project. The client demand that he can block user IP from admin panel. He can block any IP, so user will not access the site in future.

When I research on it I found two solutions

1- Using IIS

2- Using HTTP Module

1- Using IIS

This method is best for advance user. I did not work on this method. But if you are interested please read this article.

2- Using HTTP Module

This method is very easy and I used this to accomplish the client requirements. In this method, we need two things.

i- A class in App_Code Folder

ii- Setting in Web.Config file

i- A class in App_Code Folder

Here is whole code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for BlockIP
/// </summary>
public class BlockIP : IHttpModule
public BlockIP()
// TODO: Add constructor logic here

#region IHttpModule Members

public void Dispose()

public void Init(HttpApplication context)
context.BeginRequest += new EventHandler(Application_BeginRequest);

private void Application_BeginRequest(object source, EventArgs e)
HttpContext context = ((HttpApplication)source).Context;
string ipAddress = context.Request.UserHostAddress;
if (!IsValidIpAddress(ipAddress))
context.Response.StatusCode = 403; // (Forbidden)


private bool IsValidIpAddress(string ipAddress)
return UserIP.IsIpValid(ipAddress);


In the Application_beginRequest we check that if ip is invalid then generate an error. So user can not see the page. We do this in beginning of request. so that user can not view the page and never get and content.

In this method IsValidIpAddress you can verify that it is valid IP address or blocked, I check this using User class. In it I check from database, where all blocked IP addresses saved. You can also write here your custom checking.

ii- Setting in Web.Config file

Now, we add this HTTP Module in web.config file so that we can use it. Under <System.Web> tag add this

<add name=”YourClassName” type=”Your Class Name”/>

This is complete. Now you can block any user programmatically.

Pray for Me!


10 Responses to “ASP.NET – Block IP Addresses from Your Site”

  1. 1 Chris Quick September 22, 2008 at 12:21 pm

    This works great but it doesn’t show any UI to the end user. Would this have to be added programatically as well? I’m assuming a response.redirect or server.transfer would be in order to handle the status code. However, is there an easy way to get IIS to display the default error pages?

  2. 2 Eric October 6, 2008 at 10:07 pm

    Chris: You can redirect to your default error page. I redirect to a friendly error page and do the status code there.

  3. 3 emon June 26, 2009 at 9:38 pm

    Thank you very much…
    this code helped me so much!!
    sure.. that i’ll say prayer for you… :))

  4. 4 mani December 2, 2010 at 1:16 am

    thank you.

  5. 6 Richard Kemme March 14, 2012 at 11:57 pm

    Thank you a lot for providing individuals with an exceptionally marvellous chance to read in detail from this site. It can be very cool plus packed with a lot of fun for me and my office friends to visit your website really three times a week to read through the newest guides you have. And of course, I’m so usually happy with all the beautiful points you give. Certain 1 tips in this article are basically the most impressive we have ever had.

  6. 7 Cradz May 31, 2012 at 11:17 pm

    Perfect. Thanks for posting.. just what I needed to block some banned chat users who keep coming back creating new accounts.

  7. 8 Directory Siti April 15, 2013 at 2:27 pm

    My spouse and I stumbled over here different web address and thought I might check things out.

    I like what I see so i am just following you.
    Look forward to checking out your web page again.

  8. 9 Cara January 25, 2014 at 4:54 pm

    “ASP.NET – Block IP Addresses from Your Site | Rana Faisal Munir’s Blog” ended up
    being a great posting, can not help but wait to browse far
    more of ur blog posts. Time to waste a lot of time on the internet lol.

    Thanks for your effort ,Danilo

  9. 10 Valencia August 14, 2014 at 1:52 pm

    Heya i’m for the primary time here. I found this board and I
    in finding It truly helpful & it helped me out a lot.
    I am hoping to provide something back and help others like you aided

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: