Paduraksa.ProxyProtocol.AspNetCore 0.1.0

dotnet add package Paduraksa.ProxyProtocol.AspNetCore --version 0.1.0
                    
NuGet\Install-Package Paduraksa.ProxyProtocol.AspNetCore -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Paduraksa.ProxyProtocol.AspNetCore" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Paduraksa.ProxyProtocol.AspNetCore" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Paduraksa.ProxyProtocol.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Paduraksa.ProxyProtocol.AspNetCore --version 0.1.0
                    
#r "nuget: Paduraksa.ProxyProtocol.AspNetCore, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Paduraksa.ProxyProtocol.AspNetCore@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Paduraksa.ProxyProtocol.AspNetCore&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Paduraksa.ProxyProtocol.AspNetCore&version=0.1.0
                    
Install as a Cake Tool

Paduraksa.ProxyProtocol.AspNetCore

PROXY protocol (HAProxy v1 and v2) support for Kestrel. A connection middleware reads the header from a load balancer, then makes the connection's remote and local endpoints the client's, so HttpContext.Connection.RemoteIpAddress and everything built on it (rate limits, logging, IP rules) sees the real client. The parsing is Paduraksa.ProxyProtocol: strict, fuzzed and allocation-free.

using Paduraksa.ProxyProtocol.AspNetCore;

builder.WebHost.ConfigureKestrel(kestrel =>
{
    kestrel.ListenAnyIP(8080, listen =>
    {
        listen.UseProxyProtocol(options =>
        {
            // System.Net.IPNetwork in full: Microsoft.AspNetCore.HttpOverrides has an IPNetwork too.
            options.TrustedProxies = [System.Net.IPNetwork.Parse("10.0.0.0/8")]; // your load balancers
            options.HeaderTimeout = TimeSpan.FromSeconds(3);
            // Optional, for counters and logs; an exception it throws is swallowed.
            options.OnOutcome = (outcome, connection) => metrics.Count(outcome);
        });
        listen.UseHttps(certificate); // after UseProxyProtocol: the header comes before the TLS handshake
    });
});

Warning: call UseProxyProtocol before UseHttps. The load balancer's header comes first on the wire, so it has to be read before the TLS handshake. In the other order every HTTPS connection through the load balancer is closed during the TLS handshake and the app never sees a request: it looks like the proxy refusing everyone, health checks included. Kestrel doesn't let the middleware see whether UseHttps already ran, so this isn't detected for you. If every connection from the load balancer fails, check the order first.

Behaviour

  • Secure by default. TrustedProxies is empty, which refuses every connection until you add your load balancers; a warning in the log says so at startup. A connection from anywhere else is aborted without reading a byte.
  • Unix sockets have no peer address to match, so a ListenUnixSocket listener refuses every connection until you set TrustUnixSockets = true. The socket's file permissions are then the whole check: let only your proxy connect.
  • Refusals are not logged one by one. OnOutcome gets the outcome and the connection (for a refused one, connection.RemoteEndPoint is the peer that was refused), so count or log them there.
  • Required by default. A trusted peer that sends no valid header within HeaderTimeout (default 3 s), or whose header is malformed or over MaxV2Length (default 4096), is aborted.
  • Required = false makes the header optional: a trusted peer whose first bytes don't begin a header goes on unchanged, with the socket's endpoints. Untrusted peers go on unchanged too, and a header they send is not honoured. A header that begins correctly and then breaks still aborts.
  • On success the connection's RemoteEndPoint and LocalEndPoint become the header's source and destination (when it carries addresses; LOCAL and UNKNOWN keep the socket's), and IProxyProtocolFeature exposes the parsed header and the proxy's own endpoint.
  • Trust matching treats an IPv4-mapped IPv6 peer (::ffff:10.0.0.1, what a dual-mode listener reports) as IPv4.
  • QUIC / HTTP/3 has no connection middleware, so the PROXY protocol doesn't apply there.

Keep the listener reachable by the load balancer alone: anything that can connect from a trusted address can claim any client address.

Targets .NET 10 and .NET 8.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 122 11/13/2026