A complete solution for obtaining the Client IP Address

Source: Internet
Author: User
Tags servervariables

MCT Maulik Patel from India provides a server-side IP retrieval Solution

If (context. Request. servervariables ["http_via"]! = NULL) // Using proxy
{
IP = context. Request. servervariables ["http_x_forwarded_for"]. tostring (); // return real client IP.
}
Else // not using proxy or can't get the client IP
{
IP = context. Request. servervariables ["remote_addr"]. tostring (); // while it can't get the client IP, it will return proxy IP.
}

PS

1. Some proxies will not send us real IP addresses.

2. Some clients will not send us IP addresses because of the "header_access deny" security settings.

However, an error occurred while obtaining the real client IP address due to the second remark (the above solution is adjusted as follows)

The following solutions are available:

/// <Summary>

/// Obtain the remote IP Address

/// </Summary>

/// <Returns> IP address of the remote host </returns>

Public static string getcustomerip ()

{

String customerip = "";

If (httpcontext. Current. Request. servervariables ["http_via"]! = NULL) // Using proxy

{

Try

{

// Return real client IP.

Customerip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring ();

}

Catch

{

// While it can't get the client IP, it will return proxy IP.

Customerip = httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();

}

}

Else

{

// While it can't get the client IP, it will return proxy IP.

Customerip = httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();

}

Return customerip;

}

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.