0

I want to read IP address of client machine, I am using below line of code for this -

UserIP = Dns.GetHostAddresses(Dns.GetHostName())[1].ToString();

For some machines it is returning correct IP address like - 10.50.207.154 but for some machine it is returning IP address like - fe80::25ab:4248:c134:23c6%29

How to get IP address like(Ex - 10.50.207.154) first one?

juvchan
  • 6,113
  • 2
  • 22
  • 35
Nitendra Jain
  • 489
  • 1
  • 7
  • 24

2 Answers2

4

They're both IP addresses. The 'correct' one is IPv4. The longer one is IPv6.

You can write a simple regex to detect IPv4, but I have a feeling you're incorrectly dismissing IPv6 addresses as not being IP addresses, when they very much are so.

Community
  • 1
  • 1
Rob
  • 26,989
  • 16
  • 82
  • 98
1

Get IP address of client machine, try this its work for me,

private string GetIPAddress()
        {
            string IpAddress;
            IpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (IpAddress == "" || IpAddress == null)
            {
                IpAddress = Request.ServerVariables["REMOTE_ADDR"];

                if (IpAddress == "::1" || IpAddress == "localhost")
                {
                    IPAddress[] ipArray = Dns.GetHostAddresses(Dns.GetHostName());
                    foreach (IPAddress ip in ipArray)
                    {
                        //InterNetwork for IPV4
                        if (ip.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IpAddress = ip.ToString();
                            break;
                        }
                    }
                }
            }

            return IpAddress;
        }
Prabhat Sinha
  • 1,500
  • 20
  • 32