.NET 获取当前IP

获取当前用户ip 

     public string getRemoteIp()
 {
       var ip = HttpContext.Request.Headers["X-Original-For"].FirstOrDefault();
      if (string.IsNullOrEmpty(ip))
         {
             ip = "*.*.*.*";
         }
        return ip;
}

OR

   /// <summary>
        /// 获取客户Ip
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetClientUserIp(this HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }

但是我没有读取成功,很纳闷,先记录下来,后续解决,不只是浏览器还是什么缘故

posted on 2020-03-20 15:59  RookieBoy666  阅读(570)  评论(0编辑  收藏  举报