Last active
April 18, 2024 13:02
-
-
Save martyncoup/335e29456bdf60e03ea91095b16974b2 to your computer and use it in GitHub Desktop.
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
// Removed for brevity | |
builder.Services.AddAuthentication(...); | |
// Configure load balancer headers for MS Identity | |
builder.Services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, o => | |
{ | |
o.Events = new OpenIdConnectEvents | |
{ | |
// Override the redirect URI | |
OnRedirectToIdentityProvider = (context) => | |
{ | |
if (context.Request.Headers.ContainsKey("X-Forwarded-Host")) | |
{ | |
context.ProtocolMessage.RedirectUri = $"https://{context.Request.Headers["X-Forwarded-Host"]}{builder.Configuration.GetSection("AzureAd").GetValue<string>("CallbackPath")}"; | |
} | |
return Task.FromResult(0); | |
} | |
}; | |
}); | |
// Configure forwarding headers | |
builder.Services.Configure<ForwardedHeadersOptions>(o => | |
{ | |
o.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; | |
o.KnownNetworks.Clear(); | |
o.KnownProxies.Clear(); | |
}); | |
// Below the following line... | |
var app = builder.Build(); | |
// Add this before app.UseAuthentication() | |
app.UseForwardedHeaders(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment