Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AZBlocker
using System;
using System.Web;
using System.Text.RegularExpressions;
namespace AZBlocker
{
/// <summary>
/// *.azurewebsites.net access blocker
/// </summary>
public class Class1 : IHttpModule
{
private HttpApplication App;
private bool isMatched = false;
public void Init(HttpApplication context)
{
App = context;
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
context.PreSendRequestHeaders += new EventHandler(OnPreSendRequestHeaders);
context.PreSendRequestContent += new EventHandler(OnPreSendRequestContent);
}
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
string hostName = HttpContext.Current.Request.Url.Host;
// HttpContext.Current.Response.Headers.Add("Name", hostName);
if (!String.IsNullOrEmpty(hostName))
{
try
{
Regex regx = new Regex(@"^(.*).azurewebsites.net$");
// Regex regx = new Regex(@"^localhost$");
if (regx.IsMatch(hostName))
{
// App.Response.Redirect("/"); // ERR_TOO_MANY_REDIRECTS:
// App.Response.Redirect("javascript:"); // Running javascript scheme at Top-level is prohibited.
// App.Response.Redirect("script:"); // Firefox say: http Unknown protocol
// App.Response.Close(); // ERR_CONNECTION_RESET: Connection reset
// App.Response.End(); // throw ThreadAbortException
// App.Context.Session.Timeout = 0; // Exception
// App.CompleteRequest(); // Safety?
// For Azure Web Service
App.Response.StatusCode = 302;
HttpContext.Current.Response.Headers.Add("Location", "script:");
isMatched = true;
}
} catch (Exception)
{
throw;
}
}
}
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
if (isMatched)
{
HttpContext.Current.Response.Headers.Remove("Server");
HttpContext.Current.Response.Headers.Remove("Content-Type");
HttpContext.Current.Response.Headers.Remove("Last-Modified");
HttpContext.Current.Response.Headers.Remove("Etag");
HttpContext.Current.Response.Headers.Remove("Accept-Ranges");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
}
}
private void OnPreSendRequestContent(object sender, EventArgs e)
{
if (isMatched)
{
App.Context.Response.ClearContent();
}
}
public void Dispose()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.