Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for forwarding headers #11

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Konmaripo.Web/Konmaripo.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
Expand All @@ -26,11 +27,17 @@ public Startup(IConfiguration configuration)
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)
{
ConfigureHackyHttpsEnforcement(services);

IdentityModelEventSource.ShowPII = true;
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
.AddAzureAD(options =>
{
Configuration.Bind("AzureAd", options);
});

services.AddControllersWithViews(options =>
{
Expand All @@ -42,6 +49,27 @@ public void ConfigureServices(IServiceCollection services)
services.AddRazorPages();
}

/// <summary>
/// This is a workaround due to issues running in a Linux container with no reverse proxy in front of it.
/// In this situation, Azure AD authentication attempts to use a redirect URI that is http instead of https.
/// For more information: https://github.com/excellalabs/konmaripo/issues/10
/// </summary>
/// <param name="services"></param>
private void ConfigureHackyHttpsEnforcement(IServiceCollection services)
{
// HACK
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
// Only loopback proxies are allowed by default.
// Clear that restriction because forwarders are enabled by explicit
// configuration.
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Expand Down