Asp.net best way to get client IP address


Now days most of the web applications are deployed behind some proxy or on load balance. In that scenario getting client address using HTTP header may create problem. For this kind of scenario HTTP protocol supports X-Forwarded-For header parameter, this keeps all IP’s(proxy, load balancer) in sequence. So for getting correct IP address of client you can use following method.

/// <summary>
/// Gets the client\'s IP address.
/// This method takes into account the X-Forwarded-For header,
/// in case the blog is hosted behind a load balancer or proxy.
/// </summary>
/// <returns>The client\'s IP address.</returns>
public static string GetClientIP()
{
    var context = HttpContext.Current;
    if (context != null)
    {
        var request = context.Request;
        if (request != null)
        {
            string xff = request.Headers["X-Forwarded-For"];
            string clientIP = string.Empty;
            if (!string.IsNullOrWhiteSpace(xff))
            {
                int idx = xff.IndexOf(\',\');
                if (idx > 0)
                {
                    // multiple IP addresses, pick the first one
                    clientIP = xff.Substring(0, idx);
                }
                else
                {
                    clientIP = xff;
                }
            }

            return string.IsNullOrWhiteSpace(clientIP) ? request.UserHostAddress : clientIP;
        }
    }

    return string.Empty;
}