0

I'm getting client IP address from ASP.NET. But some client IP address received 127.0.0.1. What is problem. How getting valid client ip address?

I'm using this code:

    public static string GetIP()
{
    string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(clientIp))
    {
        string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        clientIp = forwardedIps[forwardedIps.Length - 1];
    }

    if (string.IsNullOrEmpty(clientIp))
        clientIp = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
    if (string.IsNullOrEmpty(clientIp))
        clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

    return clientIp.ToString();
}
Codder AE
  • 3
  • 2
  • 127.0.0.1 is the localhost address, I suppose that you are connecting to the page from the same computer on which it is hosted? – odyss-jii Jan 16 '13 at 13:48
  • just a guess? but could be proxy issues. take a look at http://stackoverflow.com/questions/7445592/what-is-the-difference-between-http-client-ip-and-http-x-forwarded-for – ddavison Jan 16 '13 at 13:49
  • sircapsolat, I think so. But I could not find how to solve. – Codder AE Jan 16 '13 at 14:02

1 Answers1

1

127.0.0.1 is localhost, i.e. the same machine is making the request as is hosting it.

My guess is that what you are seeing is infact your own testing or debugging?

I'd consider Request.IsLocal() as a good way to find out.

KingCronus
  • 4,509
  • 1
  • 24
  • 49