1

I have written the following code to get the IPv4 address of my machine. When we deploy this code on the server, this code gives me the IP address of the server, but not the machines IP on which application running?

public string getIpAddress()
{
    try
    {
        string myHost = System.Net.Dns.GetHostName();
        string myIP = null;

        for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
        {
            if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
            {
                myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
            }
        }
        return myIP;            
    }
    catch (Exception)
    {            
        throw;
    }
}
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
user3437960
  • 41
  • 2
  • 7

1 Answers1

2

This is the expected behavior as this code is running on the server side.

However, if you want to get the client IP address, you can still do it by using the Request object. (See this post for more details or check on google for examples)

Community
  • 1
  • 1
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78