0

I need help to find user's IP address who are browsing to my site (using C# Code).

I am confused about system IP address, ISP IP address, Network address and what is IPV4 and IPV6.

Below is the code I got from other source:

protected string GetUserIP()
{
        string userIP = string.Empty;
        HttpContext context = System.Web.HttpContext.Current;

        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                userIP = addresses[0];
            }
        }
        else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
        {
            userIP = HttpContext.Current.Request.UserHostAddress;
        }

        return userIP;
}

But here I am not getting the expected result.

When test this in my local-host I got 127.0.0.1

When I deploy this into my development server and browse the page in my local machine, I got different address.

I tried accessing the same URL in other machine and IP address are same.

The systems I am using to access my development server deployed page in in same network.

So is it the network IP my code returned..?

Because when I find my IP using ipconfig command it's again a different!

And when I checked my IP using 3rd party site (http://www.whatismyip.com/ip-address-lookup/), it shows the actual IP I expected. How can I get that actual IP using C# code?

My ultimate goal is, get the user location details who is browsing my site using Maxmind City DB, but for that I need to pass the IP of browsing user.

string ip = GetUserIP();
string db = “GeoIP2-City.mmdb";
var reader = new DatabaseReader(db);
var city = reader.City(ip);
double? lat = city.Location.Latitude;
double? lang = city.Location.Longitude;

For the IP I got from my above code, I got exception (IP not found in Database) when I tried to get information from Maxmind DB, but for the IP I got from "whatismyip" website, I got the details from the Maxmind DB.

Hope My question is clear. Please let me know if anyone have any inputs.

Thanks, Sharath

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sharath
  • 171
  • 1
  • 4
  • 18
  • You need your external address, which you cannot obtain when accessing the server from an internal network. – CodeCaster Jul 03 '14 at 08:55
  • have a look on this [link](http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net). – user3442830 Jul 03 '14 at 12:15

1 Answers1

0

I guess this will help

public String GetLanIPAddress()
    {
        //The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a 
        //client connecting to a web server through an HTTP proxy or load balancer
        String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (string.IsNullOrEmpty(ip))
        {
            ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        return ip;
    }