1
0

Enhanced HttpContext.GetRemoteIpAddress() extension

This commit is contained in:
2023-11-22 21:55:12 +01:00
parent ca76966827
commit 1576e78686
3 changed files with 41 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.Extensions.DependencyInjection;
@@ -13,9 +14,9 @@ namespace Microsoft.AspNetCore.Http
// Search these additional headers for a remote client ip address.
private static readonly string[] defaultIpHeaderNames = new[]
{
"X-Forwarded-For", // commonly used on all known proxies
"Cf-Connecting-Ip", // set by Cloudflare
"X-Real-IP", // wide-spread alternative to X-Forwarded-For
"CF-Connecting-IP" // set by Cloudflare
"X-Forwarded-For", // commonly used on all known proxies
};
/// <summary>
@@ -37,9 +38,9 @@ namespace Microsoft.AspNetCore.Http
/// <remarks>
/// Searches for additional headers in the following order:
/// <list type="number">
/// <item>X-Forwarded-For</item>
/// <item>Cf-Connecting-Ip</item>
/// <item>X-Real-IP</item>
/// <item>CF-Connecting-IP</item>
/// <item>X-Forwarded-For</item>
/// </list>
/// </remarks>
/// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
@@ -57,7 +58,11 @@ namespace Microsoft.AspNetCore.Http
if (!httpContext.Request.Headers.ContainsKey(headerName))
continue;
forwardedForAddress = httpContext.Request.Headers[headerName].ToString();
// X-Forwarded-For can contain multiple comma-separated addresses.
forwardedForAddress = httpContext.Request.Headers[headerName].ToString()
.Split(',', StringSplitOptions.TrimEntries)
.First();
break;
}
@@ -73,9 +78,9 @@ namespace Microsoft.AspNetCore.Http
/// <remarks>
/// Searches for additional headers in the following order:
/// <list type="number">
/// <item>X-Forwarded-For</item>
/// <item>Cf-Connecting-Ip</item>
/// <item>X-Real-IP</item>
/// <item>CF-Connecting-IP</item>
/// <item>X-Forwarded-For</item>
/// </list>
/// </remarks>
/// <param name="httpContext">The current <see cref="HttpContext"/>.</param>