/// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {
            try
            {
                string result = String.Empty;

                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }

                if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
                {
                    return "127.0.0.1";
                }

                return result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return HttpContext.Current.Request.UserHostAddress;
            }
        }

 

相关文章:

  • 2022-12-23
猜你喜欢
  • 2021-07-01
  • 2021-06-14
  • 2021-12-14
  • 2021-09-04
  • 2021-10-02
相关资源
相似解决方案