-1

I believe this question may be quickly flagged as a duplicate. However, I do believe this is a legitimate question.

At this time, I'm getting my current IP address using the following code:

IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress currentIpAddress = hostEntry.AddressList.FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork);
Console.WriteLine(currentIpAddress.ToString());

When I run this code, I get the first Ethernet adapter value I see when I run ipconfig from a command-line. However, this value is different than the IP address I see when I run a query for "what is my ip address" on Google. I don't understand why these values are different. For that reason, I'm concerned that my code is incorrect.

How do I know if my code is correctly getting the IP address of the machine the app is currently running on?

Some User
  • 5,257
  • 13
  • 51
  • 93
  • If you need the external IP address, you need to call out to an internet service to get that: https://stackoverflow.com/questions/3253701/get-public-external-ip-address – John Koerner May 12 '18 at 12:58

1 Answers1

1

I guess what you're seeing is your local IP address in your network. It is like 192.168.x.x, right?

From your local PC you can't see your "outside" IP address. Its known to your router or you have to query a service that displays your IP address. So, you have to find some kind of service you can ask via REST or load a webpage which displays your IP address and parse the HTML.

Niklas
  • 102
  • 10
  • No. I'm seeing a 172.x.x.x address. – Some User May 12 '18 at 13:09
  • Okay, doesn't matter. I tried your code snipped - you're always receiving the local address. Please check the solutions given in the linked question (the one yours is the duplicate of)! – Niklas May 12 '18 at 13:20