Skip to content

Instantly share code, notes, and snippets.

@AaronSadlerUK
Created March 19, 2021 10:25
Show Gist options
  • Save AaronSadlerUK/4393c4e7e6f3de41edb4eaf09c47a80e to your computer and use it in GitHub Desktop.
Save AaronSadlerUK/4393c4e7e6f3de41edb4eaf09c47a80e to your computer and use it in GitHub Desktop.
How to fix Request.Url.Scheme returning http instead of https on load balanced site - Blog post: https://aaronsadler.dev/2021/march/19/how-to-fix-request-url-scheme-returning-http-instead-of-https-on-load-balanced-site/
public sealed class HttpOverrides : IHttpModule
{
void IHttpModule.Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
}
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string forwardedFor = app.Context.Request.Headers["X-Forwarded-For"]?.Split(new char[] { ',' }).FirstOrDefault();
if (forwardedFor != null)
{
app.Context.Request.ServerVariables["REMOTE_ADDR"] = forwardedFor;
app.Context.Request.ServerVariables["REMOTE_HOST"] = forwardedFor;
}
string forwardedProto = app.Context.Request.Headers["X-Forwarded-Proto"];
if (forwardedProto == "https")
{
app.Context.Request.ServerVariables["HTTPS"] = "on";
app.Context.Request.ServerVariables["SERVER_PORT"] = "443";
app.Context.Request.ServerVariables["SERVER_PORT_SECURE"] = "1";
}
}
void IHttpModule.Dispose()
{
}
}
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="HttpOverrides" type="Namespace.HttpOverrides" preCondition="integratedMode" />
</modules>
</system.webServer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment