IP Address Restriction via HttpModules

Sometimes, you get some pesky visitors that just wont go away, and you want to just restrict the access from the site. If you run your .net site on a shared environment (like I do), you cant simply go into IIS and set up the deny list, you'll have to find another way (Just to be really annoying, I'm using my old employer's ip address for this demo)

Heres one I prepared earlier

class Restriction : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
 
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            string ipAddress = context.Request.UserHostAddress;
            if (!IsValidIpAddress(ipAddress))
            {
                context.Response.StatusCode = 403;  // (Forbidden)
                context.Response.Write("This site is forbidden from this IP address. Please contact your network administrator.");
                context.Response.End();
            }
        }
 
        private bool IsValidIpAddress(string address)
        {
            if (address.StartsWith("195.172.10"))
            {
                return false;
            }
            return true;
        }
 
        public void Dispose()
        {
        }
    }