Last active
December 17, 2015 01:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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