Is there a simple way to apply a filter on action methods in ASP.NET MVC3 against specific types of UserAgents? I have a hacking network chewing on us in different ways. I can play cat and mouse with IPs, subnets, etc at the network/firewall level but would like to inject an app level assurance against things like Squid, etc as they appear to have certain patterns that arise. Not sure how this would affect performance but wondered if anyone has done this approach.
thanks in advance. doug
You would have to create a custom action filter. Here is an example of one that returns an http 403 forbidden if the requesting user agent is a mozilla-based browser:
public class UserAgentActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.UserAgent.ToLowerInvariant().Contains("mozilla"))
{
filterContext.Result = new HttpStatusCodeResult(403);
}
base.OnActionExecuting(filterContext);
}
}
Just remember that user-agents can be faked if they realize that is how they are being blocked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With