Allow only requests from local machine in WebAPI 2

asp.net-web-api, c#

Solution

FROM book "Pro ASP.NET MVC 4 4th edition":

public class CustomActionAttribute : FilterAttribute, IActionFilter { 
    public void OnActionExecuting(ActionExecutingContext filterContext) { 
        if (filterContext.HttpContext.Request.IsLocal) { 
            filterContext.Result = new HttpNotFoundResult(); 
        } 
    }

    public void OnActionExecuted(ActionExecutedContext filterContext) { 
        // not yet implemented 
    } 
}

Problem

There are some action methods in my WebAPI 2 application where I would like to disable remote accessibility (scheduled administrative tasks). Other action methods should be publicly available. Is an `ActionFilter` the best bet in this case?

Original source

Related problems