Created
March 19, 2021 10:25
-
-
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/
This file contains hidden or 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
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() | |
{ | |
} | |
} |
This file contains hidden or 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
<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