Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Misc

Restrict access to ASPRunner.NET application by IP address

Scroll Prev Next More

 

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.

Allow local access only:

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:

Security screen

Advanced security settings

Registration and passwords

Active Directory