0

How to block Ip ranges using http modules with c# . I have a simple solution but its for single Ip and How can I read IPs from xml file in http modules so that if an IP is there in that file it will be blocked .Here is my code

public class MyHandler :IHttpModule
{
 public MyHandler(){}

    public bool IsReusable
    {
        get { return false; }
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(Application_BeginRequest);
    }
    private void Application_BeginRequest(object source, EventArgs e)
    {
        HttpContext context = (source as HttpApplication).Context;
        List<string> BlackListIp = new List<string>();
        BlackListIp.Add("127.0.0.1");

        if (BlackListIp.Contains(context.Request.UserHostAddress))
        {
            context.Response.Write("<h1 style='color:red;'>Your IP is Blocked</h1>");
        }
    }

    public void Dispose()
    {
    }
}
vcsjones
  • 138,677
  • 31
  • 291
  • 286
HBK
  • 67
  • 2

1 Answers1

0

Just add the xml file inside app_data folder and read it using XDocument. You can event use Cache with FileDependency to improve performance.

Martino Bordin
  • 1,412
  • 1
  • 14
  • 29