示例#1
0
        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <returns>ClientIP</returns>
        public static string GetClientIP(HttpContext context)
        {
            // 通过服务端转发的真实客户端IP
            string ip = context.Request.Headers["clientIp"];

            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = context.Request.Headers["x-forwarded-for"];
            }

            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = context.Request.Headers["Proxy-Client-IP"];
            }

            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = context.Request.Headers["WL-Proxy-Client-IP"];
            }

            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }

            ip = IpUtils.ValidClientIP(ip);
            return(ip);
        }