1

I would like to serve another content depending on whether a user is using a proxy server or not.

        if(FROM_PROXY){
          routes.MapRoute(
            name: "ProxyDefault",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "HomeProxy",
              action = "Index", id = UrlParameter.Optional 
            }
          );
        }

So, how to detect if my asp.net mvc application is accessed via a proxy?

Bellash
  • 7,560
  • 6
  • 53
  • 86
  • 1
    Some proxies are transparent so there's no way to be 100% sure. – DavidG Feb 05 '15 at 11:42
  • 2
    Routes registration are performed once at application startup, at this stage no HTTP request is present and no way to determine proxy. – haim770 Feb 05 '15 at 11:43
  • Why do you want to route proxied users to another controller? Do you need different behavior? Different view? – haim770 Feb 05 '15 at 11:46
  • @haim770 thank you but It's just an example! What I really need is to detect if user is on a proxy, so I can prevent the user... – Bellash Feb 05 '15 at 11:56
  • By "preventing the user" you mean that you want to return an "401 Unauthorized" response? Or redirect the user to another error page? – haim770 Feb 05 '15 at 12:18
  • redirect to another page for example – Bellash Feb 05 '15 at 13:23

1 Answers1

0

You can register the following global filter:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class BlockProxyAccessAttribute : ActionFilterAttribute
{
    private static readonly string[] HEADER_KEYS = { "VIA", "FORWARDED", "X-FORWARDED-FOR", "CLIENT-IP" };
    private const string PROXY_REDIR_URL = "/error/proxy";

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var isProxy = filterContext.HttpContext.Request.Headers.AllKeys.Any(x => HEADER_KEYS.Contains(x));

        if (isProxy)
            filterContext.Result = new RedirectResult(PROXY_REDIR_URL);
    }
}

(The candidate header keys for proxy determination are taken from this answer)

Community
  • 1
  • 1
haim770
  • 48,394
  • 7
  • 105
  • 133