SmarterTools - here the code for .NET that enables the option to configure trusted proxy IP adresses in CIDR-format like 192.168.25.10/32:
First an extension method ->
using System.Net;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
namespace Xperion.BusinessCenter.Extensions;
public static class NetworkExtensions
{
public static IPNetwork ToIpNetwork(this string cidr)
{
try
{
var delimiterIndex = cidr.IndexOf('/');
var ipSubnet = cidr.Substring(0, delimiterIndex);
var mask = cidr.Substring(delimiterIndex + 1);
var prefixLength = int.Parse(mask);
var subnetAddress = IPAddress.Parse(ipSubnet);
return new IPNetwork(subnetAddress, prefixLength);
}
catch
{
return new IPNetwork(IPAddress.Parse("127.0.0.1"), 32);
}
}
}Then the service configuration for ForwardedHeaderOptions (found in Microsoft.AspNetCore.HttpOverrides.dll) which gets the known trusted proxy IP in CIDR-format from the configuration - where else you have it.
services.Configure<ForwardedHeadersOptions>(options =>
{
var knownProxyNetworks = configuration.GetValue("KnownProxyNetworks", "").Split(new[]{',',';'}, StringSplitOptions.RemoveEmptyEntries);
var networkList = knownProxyNetworks.Select(x => x.ToIpNetwork());
options.ForwardedHeaders = ForwardedHeaders.All;
options.KnownNetworks.AddRange(networkList);
});Let's implement it so the reel ip of the clients are shown in logfiles, logged in users overview etc.
Thanks :-)