After some research, I determined that this is probably the most appropriate answer. It can be updated to provide json, text or xml, although the specification indicates that HTML is recommended.
public class RequireHttpsAttribute : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext context) { if (context.Request.RequestUri.Scheme != Uri.UriSchemeHttps) { context.Response = new HttpResponseMessage(HttpStatusCode.UpgradeRequired); context.Response.Headers.Add("Upgrade", "TLS/1.1, HTTP/1.1"); context.Response.Headers.Add("Connection", "Upgrade"); context.Response.Headers.Remove("Content-Type"); context.Response.Headers.Add("Content-Type", "text/html"); context.Response.Content = new StringContent("<html><head></head><body><h1>Http protocol is not valid for this service call.</h1><h3>Please use the secure protocol https.</h3></body></html>"); } else base.OnAuthorization(context); } }
Here is the specification: RFC 2817
N-ate Sep 25 '17 at 0:53 on 2017-09-25 00:53
source share