Restricting or allowing access by an IP address is an easy task. Here are a few examples. The code needs to be added to the beginning of the AfterAppInit event.
Note: This tutorial uses IPv4 addresses.
if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]!="127.0.0.1") {
HttpContext.Current.Response.End();
}
Allow access from the list of approved IP addresses:
string[] addresses = {"127.0.0.1", "96.24.12.122", "96.24.12.123"};
if (!addresses.Contains(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])) {
HttpContext.Current.Response.End();
}
Restrict access from a certain IP address:
if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]=="127.0.0.1") {
HttpContext.Current.Response.End();
}
Restrict access from the list of addresses:
string[] addresses = {"127.0.0.1", "96.24.12.122", "96.24.12.123"};
if (addresses.Contains(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])) {
HttpContext.Current.Response.End();
}
See also: