Based on the article Working with SSL in Web API I implemented an authorization filter to require SSL for a method of a Web API (2.1) Controller:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true,
AllowMultiple = false)]
public sealed class RequireHttpsAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
ReasonPhrase = "HTTPS Required"
};
}
else
{
base.OnAuthorization(actionContext);
}
}
}
This works fine - on some web servers. If Web Farm Framework (WFF) is used as a reverse proxy, it can fail (by blocking valid HTTPS requests).
WFF adds the header X-Forwarded-Proto
, which is a de facto standard for reverse proxies.
How can I revise this code to work with or without a standard proxy?