diff --git a/framework/Furion.Pure/AspNetCore/Extensions/HttpContextExtensions.cs b/framework/Furion.Pure/AspNetCore/Extensions/HttpContextExtensions.cs index acd19f2c0e6..95d79e44fe0 100644 --- a/framework/Furion.Pure/AspNetCore/Extensions/HttpContextExtensions.cs +++ b/framework/Furion.Pure/AspNetCore/Extensions/HttpContextExtensions.cs @@ -116,10 +116,19 @@ public static string GetLocalIpAddressToIPv6(this HttpContext context) /// 获取远程 IPv4地址 /// /// + /// 是否优先取 X-Forwarded-For /// - public static string GetRemoteIpAddressToIPv4(this HttpContext context) + public static string GetRemoteIpAddressToIPv4(this HttpContext context, bool xff = false) { - return context.Connection.RemoteIpAddress?.MapToIPv4()?.ToString(); + var ipv4 = context.Connection.RemoteIpAddress?.MapToIPv4()?.ToString(); + + if (xff) + { + var xForwardedFor = context.Request.Headers["X-Forwarded-For"]; + return !string.IsNullOrWhiteSpace(xForwardedFor) ? xForwardedFor : ipv4; + } + + return ipv4; } /// diff --git a/framework/Furion/AspNetCore/Extensions/HttpContextExtensions.cs b/framework/Furion/AspNetCore/Extensions/HttpContextExtensions.cs index acd19f2c0e6..95d79e44fe0 100644 --- a/framework/Furion/AspNetCore/Extensions/HttpContextExtensions.cs +++ b/framework/Furion/AspNetCore/Extensions/HttpContextExtensions.cs @@ -116,10 +116,19 @@ public static string GetLocalIpAddressToIPv6(this HttpContext context) /// 获取远程 IPv4地址 /// /// + /// 是否优先取 X-Forwarded-For /// - public static string GetRemoteIpAddressToIPv4(this HttpContext context) + public static string GetRemoteIpAddressToIPv4(this HttpContext context, bool xff = false) { - return context.Connection.RemoteIpAddress?.MapToIPv4()?.ToString(); + var ipv4 = context.Connection.RemoteIpAddress?.MapToIPv4()?.ToString(); + + if (xff) + { + var xForwardedFor = context.Request.Headers["X-Forwarded-For"]; + return !string.IsNullOrWhiteSpace(xForwardedFor) ? xForwardedFor : ipv4; + } + + return ipv4; } ///