public string GetOriginalClientIPAddress(HttpContext context) { string originalClientIPAddress = context.Request.Headers["X-Forwarded-For"]; if (!string.IsNullOrEmpty(originalClientIPAddress)) { // 通常 X-Forwarded-For 头可能包含多个 IP 地址,最后一个通常是原始客户端的 IP string[] ipAddresses = originalClientIPAddress.Split(','); if (ipAddresses.Length > 0) { return ipAddresses[ipAddresses.Length - 1].Trim(); } } // 如果没有 X-Forwarded-For 头,或者解析失败,返回 RemoteIpAddress return context.Connection.RemoteIpAddress?.ToString(); }