Skip to content

Instantly share code, notes, and snippets.

@reinforchu
Created February 9, 2020 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinforchu/4ccbcd499626ce71e7447b0a39993567 to your computer and use it in GitHub Desktop.
Save reinforchu/4ccbcd499626ce71e7447b0a39993567 to your computer and use it in GitHub Desktop.
localhostBlocker
using System;
using System.Web;
using System.Text.RegularExpressions;
namespace localhostBlocker
{
/// <summary>
/// localhost access blocker
/// </summary>
public class Blocker : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
}
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
string hostName = HttpContext.Current.Request.Url.Host;
if (!String.IsNullOrEmpty(hostName))
{
try
{
Regex regx = new Regex(@"^localhost$");
if (regx.IsMatch(hostName))
{
HttpContext.Current.Response.Close();
}
}
catch (Exception)
{
throw;
}
}
}
public void Dispose()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment