+1  A: 

This is an example of a problem that can be solved with an AOP (Aspect-Oriented Programming) solution. For this type of thing I usually recommend PostSharp.

Basically what PostSharp allows you to do is create attributes that you can use as markers for places in your code that you wish to insert boilerplate code.

Andrew Hare
+2  A: 

You could create a custom filter attribute:

public class InternalOnly : FilterAttribute
{
    public void OnAuthorization (AuthorizationContext filterContext)
    {
     if (!IsIntranet (filterContext.HttpContext.Request.UserHostAddress))
     {
      throw new HttpException ((int)HttpStatusCode.Forbidden, "Access forbidden.");
     }
    }

    private bool IsIntranet (string userIP)
    {
     // match an internal IP (ex: 127.0.0.1)
     return !string.IsNullOrEmpty (userIP) && Regex.IsMatch (userIP, "^127");
    }
}
Todd Smith
I can't get this to work.. I've written my code exactly as you have, but the attribute doesn't fire/has no effect.... Any idea?
Alex
Got this to work with an ActionFilterAttribute.
Alex