IpHelper.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 
  2. namespace Ant.Service.Utilities
  3. {
  4. /// <summary>
  5. /// 共用工具类
  6. /// </summary>
  7. public static class IpHelper
  8. {
  9. #region 获得用户IP
  10. /// <summary>
  11. /// 获得用户IP
  12. /// </summary>
  13. public static string GetUserIp()
  14. {
  15. string ip;
  16. string[] temp;
  17. bool isErr = false;
  18. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
  19. ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
  20. else
  21. ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
  22. if (ip.Length > 15)
  23. isErr = true;
  24. else
  25. {
  26. temp = ip.Split('.');
  27. if (temp.Length == 4)
  28. {
  29. for (int i = 0; i < temp.Length; i++)
  30. {
  31. if (temp[i].Length > 3) isErr = true;
  32. }
  33. }
  34. else
  35. isErr = true;
  36. }
  37. if (isErr)
  38. return "1.1.1.1";
  39. else
  40. return ip;
  41. }
  42. #endregion
  43. }
  44. }