-1

I try type ipconfig on cmd.exe and ip address ="172.24.70.68"

But If i get IP of this PC, it return IP: 127.0.0.1

This my code get IP Address:

           IPAddress ip = null;
            IPAddress mask = null;
            //++**********************************
            // Get IP
            //--**********************************
            strHostName = Dns.GetHostName();
            IPHostEntry iphe = Dns.GetHostEntry(strHostName);
            foreach (IPAddress ipheal in iphe.AddressList)
            {
                if (ipheal.AddressFamily ==AddressFamily.InterNetwork)
                {
                    ip = ipheal;
                    break;
                }
            }

Why IP Address return value 127.0.0.1?

Some other PCs, it getting IP is ok.

enter image description here

D T
  • 3,522
  • 7
  • 45
  • 89

2 Answers2

1

Try not to get the address via DNS, which may be deceiving or simply not working if for example there are no DNS records for the computer, but via the adapter settings, which is essentially the same ipconfig does.

You can get all of the adapters with NetworkInterface.GetAllNetworkInterfaces(). The NetworkInterfaceType property let's you filter for Ethernet adapters as well as exclude the loop back adapter. You can also filter only for adapters in a particular status, e.g. up, with the OperationalStatus property.

You can then loop through all the unicast addresses of the adapter and pick one of the IPv4 addresses from it, for example the first one encountered. Of course, if you have more adapters or addresses on one adapter this might still not be the one you're looking for. In that case you need to define how to recognize the one you want and implement that accordingly.

IPAddress ip = null;
IPAddress mask = null;

foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
    bool found = false;

    if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet
        && networkInterface.OperationalStatus == OperationalStatus.Up
        && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
    {
        foreach (UnicastIPAddressInformation unicastIPAddressInformation in networkInterface.GetIPProperties().UnicastAddresses)
        {
            if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
            {
                ip = unicastIPAddressInformation.Address;
                mask = unicastIPAddressInformation.IPv4Mask;
                found = true;
                break;
            }
        }
    }

    if (found)
    {
        break;
    }
}
sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • I use this method to get IP, https://stackoverflow.com/questions/6803073/get-local-ip-address. Is it not correct ? – D T May 17 '19 at 11:55
  • @DT: Well, at least it's not doing what `ipconfig` does. And it seemed to me you want the figures `ipconfig` presents. Maybe you should familiarize yourself with what a network adapter is and what settings it can have and what DNS does in detail to better understand the difference. – sticky bit May 17 '19 at 11:59
0

You need to use forwarder Header .net core example

  services.Configure<ForwardedHeadersOptions>(options =>
 {
     options.ForwardLimit = 2;
     options.KnownProxies.Add(IPAddress.Parse("127.0.10.1"));
     options.ForwardedForHeaderName = "X-Forwarded-For-My-Custom-Header-Name";
     options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | 
     ForwardedHeaders.XForwardedProto;
 });
Shantanu
  • 554
  • 2
  • 9