Skip to content

Commit

Permalink
Add forwarded headers middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ondryaso committed Feb 15, 2024
1 parent bc6d559 commit 6116e8f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions KachnaOnline.App/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Net;
using KachnaOnline.App.DateHandling;
using KachnaOnline.App.Extensions;
using KachnaOnline.Business.Extensions;
Expand All @@ -7,6 +8,7 @@
using KachnaOnline.Business.Data.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -15,6 +17,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Serilog;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;

namespace KachnaOnline.App
{
Expand Down Expand Up @@ -113,6 +116,14 @@ public void ConfigureServices(IServiceCollection services)

// Add OpenAPI document service.
services.AddCustomSwaggerGen();

// Add reverse proxy forwarded header options
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("172.16.0.0"), 12));
});
}

/// <summary>
Expand All @@ -134,6 +145,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});
}

if (env.IsProduction())
{
app.UseForwardedHeaders();
}

app.UsePathBase("/kachna/api");

if (env.IsDevelopment())
Expand Down

0 comments on commit 6116e8f

Please sign in to comment.