Skip to content

Instantly share code, notes, and snippets.

@jcracknell
Last active December 17, 2015 01:29
Show Gist options
  • Save jcracknell/5528490 to your computer and use it in GitHub Desktop.
Save jcracknell/5528490 to your computer and use it in GitHub Desktop.
Incredibly simple redirection to equivalent SSL-secured URI. This is intended to be configured on a dedicated site listening on port 80, and will blindly redirect all GET requests to the same URL with the `https` protocol, and `403.4 SSL Required` everything else.
using System;
using System.Web;
namespace SslRedirection {
public class SslRedirectionHttpHandler : IHttpHandler {
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) {
if ("GET".Equals(context.Request.HttpMethod, StringComparison.Ordinal)) {
context.Response.StatusCode = 301;
context.Response.RedirectLocation = "https://" + context.Request.Url.Host + context.Request.RawUrl;
} else {
context.Response.StatusCode = 403;
context.Response.SubStatusCode = 4;
}
context.Response.Flush();
context.Response.Close();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<handlers>
<clear />
<add name="SslRedirectHandler" type="SslRedirection.SslRedirectionHttpHandler, SslRedirection" path="*" verb="*" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment