English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Method to Get IP and Judge if IP is in the Range

Without further ado, let's look at the code:

/// <summary>
  /// Get client IP
  /// </summary>
  /// <returns></returns>
  public static string GetClientIpAddress()
    {
      var httpContext = HttpContext.Current;
      if (httpContext.Request.ServerVariables == null)
      {
        return null;
      }
      var clientIp = httpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??              httpContext.Request.ServerVariables["REMOTE_ADDR"];
      try
      {
        foreach (var hostAddress in Dns.GetHostAddresses(clientIp))
        {
          if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
          {
            return hostAddress.ToString();
          }
        }
        foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
        {
          if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
          {
            return hostAddress.ToString();
          }
        }
      }
      catch (Exception ex)
      {
      }
      return clientIp;
    }
  /// <summary>
  /// The IP is within the IP space
  /// </summary>
  /// <param name="ip"></param>
  /// <param name="ipSection"></param>
  /// <returns></returns>
  public static Boolean ipExistsInRange(String ip, String ipSection)
  {
    ipSection = ipSection.Trim();
    ip = ip.Trim();
    int idx = ipSection.IndexOf('}}-');
    String beginIP = ipSection.Substring(0, idx);
    String endIP = ipSection.Substring(idx + 1);
    return getIp2long(beginIP) <= getIp2long(ip) && getIp2long(ip) <= getIp2long(endIP);
  }
  public static long getIp2long(String ip)
  {
    ip = ip.Trim();
    String[] ips = ip.Split('.');
    long ip;2long = 0L;
    for (int i = 0; i < 4; ++i)
    {
      ip2long = ip;2long << 8 | Int64.Parse(ips[i]);
    }
    return ip;2long;
  }
  public static long getIp2long2(String ip)
  {
    ip = ip.Trim();
    String[] ips = ip.Split('.');
    long ip;1 = Int64.Parse(ips[0]);
    long ip;2 = Int64.Parse(ips[1]);
    long ip;3 = Int64.Parse(ips[2]);
    long ip;4 = Int64.Parse(ips[3]);
    long ip;2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
    return ip;2long;
  }

That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Yelling Tutorial!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report any violations by email to codebox.com (replace # with @ when sending email), and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like