Recover External IP without third-party services

2

I have a WPF application. I need to know the external IP of the machine that is running this application.

I found some tutorials that teach to do this through the method DownloadString() of class WebClient consuming some services like dyndns.org , icanhazip.com , finally several services.

It would look something like this:

string meuIp = new WebClient().DownloadString("http://icanhazip.com");  

To use such services I need to implement the query in at least a couple of three to ensure a contingency in case some service stops working or is discontinued.

Does anyone know if it is possible to do this natively with .Net Framework without using third-party services?

    
asked by anonymous 18.11.2016 / 19:33

2 answers

8
  

[...] Is it possible to do this natively with .Net Framework without using third-party services?

No, it is not possible. Your machine has access only to the local IP, provided by the internal DNS service or set manually.

External IP is, for the most part, the one provided by your ISP to be accessible via Network Address Translation (NAT), as in the image below:

NAT categorization according to RFC , Wikipedia

In this case you will always need an external service that tells you from his point of view , with which IP you are connecting.

    
18.11.2016 / 20:04
0

You can use IP Public Knowledge .

install the NuGet package:

  

Install-Package IpPublicKnowledge

An example usage would be this:

var ip = IPK.GetMyPublicIp();
var IPinfo = IPK.GetIpInfo(ip);

Console.WriteLine("IP Publico: " + IPinfo.IP);
Console.WriteLine("ISP: " + IPinfo.isp);
Console.WriteLine("País: " + IPinfo.country);

Another way, as you yourself said, would be to use some external site, for example:

public static void Main(string[] args)
{
    string externalip = new WebClient().DownloadString("https://api.ipify.org");            
    Console.WriteLine(externalip);
}

For this case, this answer shows more examples to ensure contingency.

For other examples, this answer has several ways to do this, including one of how to create your own Http Server for this.

    
18.11.2016 / 20:03