Obtain the IP address and determine whether the IP address is in the interval. Obtain the ip address judgment interval.

Source: Internet
Author: User
Tags servervariables

Obtain the IP address and determine whether the IP address is in the interval. Obtain the ip address judgment interval.

/// <Summary> /// obtain the 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> // whether the ip address is in the ip address 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 ip2long = 0L; for (int I = 0; I <4; ++ I) {ip2long = ip2long <8 | Int64.Parse (ips [I]);} return ip2long;} public static long getIp2long2 (String ip) {ip = ip. trim (); String [] ips = ip. split ('. '); long ip1 = Int64.Parse (ips [0]); long ip2 = Int64.Parse (ips [1]); long ip3 = Int64.Parse (ips [2]); long ip4 = Int64.Parse (ips [3]); long ip2long = 1L * ip1 * 256*256*256 + ip2 * 256*256 + ip3 * 256 + ip4; return ip2long ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.