using Microsoft.AspNetCore.Http; using System.Linq; namespace YBEE.EQM.Core; public static class IPUtil { /// /// 获取请求的ip4 /// /// /// public static string GetRequestIPv4(this HttpContext context) { string ip = string.Empty; if (context.Connection.RemoteIpAddress != null) { if (context.Request.Headers.ContainsKey("X-Real-IP")) { ip = context.Request.Headers["X-Real-IP"].FirstOrDefault(); } if (context.Request.Headers.ContainsKey("X-Forwarded-For")) { ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault(); } if (string.IsNullOrEmpty(ip)) { ip = context.Connection.RemoteIpAddress?.MapToIPv4()?.ToString(); } } return ip; } }