Question

Specifically, I am creating a PDF using Essential Objects HtmlToPdf.

When the user clicks the "Create" button, I need to get the non-NAT ip address of the server (192.168.x.x) so that I can force it to create the PDF from that address, instead of the host name.

The overall problem is: on the web server, the host name for the site doesn't actually get you to the site because the DNS is specifying the external IP address which somehow is causing the connection to get blocked. So, for example-- www.testsite.com is translating to the public IP address, so the PDF creator on the server doesn't have access to that.

I created a host file entry forcing www.testsite.com to go to the internal ip. But that's a nightmare for multiple installations.

I tried

string hostName = HttpContext.Current.Request.Url.Host;
  IPHostEntry ipAddressList = Dns.GetHostEntry(hostName);
  string ipAddress = "";
  foreach (IPAddress ip in ipAddressList.AddressList)
  {
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
      ipAddress = ip.ToString();
      break;
    }
  }

But that returns the DNSed IP (external) and then the firewall NATs that to the IP I want.

Was it helpful?

Solution

var ips = NetworkInterface.GetAllNetworkInterfaces()
            .Where(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback)
            .Where(x => x.OperationalStatus == OperationalStatus.Up)
            .SelectMany(x => x.GetIPProperties().UnicastAddresses)
            .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
            .Select(x => x.Address)
            .ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top