Created
February 9, 2020 02:51
-
-
Save reinforchu/4ccbcd499626ce71e7447b0a39993567 to your computer and use it in GitHub Desktop.
localhostBlocker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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