火焰

valeb
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET WEB API NGINX代理 获取客户端IP

Posted on 2023-12-15 16:45  valeb  阅读(23)  评论(0编辑  收藏  举报
        public string RemoteIpAddress
        { 
            get
            {
                string ip4 = Request.Headers["X-Forwarded-For"].FirstOrDefault()??"";
                if (string.IsNullOrWhiteSpace(ip4))
                {
                    var ip = Request.HttpContext.Connection.RemoteIpAddress;
                    if (ip != null)
                    {
                        ip4 = ip.MapToIPv4().ToString();
                    }
                } 
                return ip4; 
            }
        }