#region Get the Web client IP
<summary>
Get IP Address
</summary>
<returns></returns>
public static string Getipaddress ()
{
string IPv4 = String.Empty;
foreach (IPAddress IP in dns.gethostaddresses (Getclientip ()))
{
if (IP. addressfamily.tostring () = = "Internetwork")
{
IPv4 = IP. ToString ();
Break
}
}
if (IPv4! = String.Empty)
{
return IPv4;
}
Using the Dns.gethostentry method, the Dns records are retrieved from the obtained IPV6 address,
One by one to determine which is the IPV4 protocol, can be converted to IPV4 address.
foreach (IPAddress IP in Dns.gethostentry (Getclientip ()). AddressList)
foreach (IPAddress IP in dns.gethostaddresses (Dns.gethostname ()))
{
if (IP. addressfamily.tostring () = = "Internetwork")
{
IPv4 = IP. ToString ();
Break
}
}
return IPv4;
}
private static string Getclientip ()
{
if (null = = httpcontext.current.request.servervariables["Http_via "])
{
return httpcontext.current.request.servervariables[ "REMOTE_ADDR"];
}
Else
{
return httpcontext.current.request.servervariables["Http_x_forwarded_for"];
}
}
#endregion
C # get Web client IP