Obtain the real IP Address

Source: Internet
Author: User
Tags servervariables
/** // <Summary>
/// Obtain the real IP address of the client. If a proxy exists, the first non-Intranet address is used.
/// </Summary>
Public static string IPAddress
{
Get
{
String result = String. Empty;

Result = HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"];
If (result! = Null & result! = String. Empty)
{
// A proxy may exist.
If (result. IndexOf (".") =-1) // No "." is definitely not in IPv4 format.
Result = null;
Else
{
If (result. IndexOf (",")! =-1)
{
// There are ",", and multiple proxies are estimated. Obtain the first IP address that is not an intranet IP address.
Result = result. Replace ("", ""). Replace ("'","");
String [] temparyip = result. Split (",;". ToCharArray ());
For (int I = 0; I <temparyip. Length; I ++)
{
If (IsIPAddress (temparyip [I])
& Temparyip [I]. Substring (0, 3 )! = "10 ."
& Temparyip [I]. Substring (0, 7 )! = "192.168"
& Temparyip [I]. Substring (0, 7 )! = "172.16 .")
{
Return temparyip [I]; // locate an address that is not an intranet address
}
}
}
Else if (IsIPAddress (result) // The proxy is in the IP Format
Return result;
Else
Result = null; // the content in the proxy is not an IP address and the IP address is used.
}

}
String IpAddress = (HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]! = Null & HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]! = String. Empty )? HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]: HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];

If (null = result | result = String. Empty)
Result = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];

If (result = null | result = String. Empty)
Result = HttpContext. Current. Request. UserHostAddress;

Return result;
}
}

/** // <Summary>
/// Determine whether the IP address format is 0.0.0.0
/// </Summary>
/// <Param name = "str1"> IP address to be determined </param>
/// <Returns> true or false </returns>
Public static bool IsIPAddress (string str1)
{
If (str1 = null | str1 = string. Empty | str1.Length <7 | str1.Length> 15) return false;
String regformat = @ "^ \ d {1, 3} [\.] \ d {1, 3} [\.] \ d {1, 3} [\.] \ d {1, 3} $ ";

Regex regex = new Regex (regformat, RegexOptions. IgnoreCase );
Return regex. IsMatch (str1 );
}

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.