17

I am trying to get the IP address of client machine using C#. I am using the below code to get the IP address :

string IPAddress = HttpContext.Current.Request.UserHostAddress;

But it is giving me the response in encoded format i.e fe80::ed13:dee2:127e:1264%13

How can I get the actual IP address? Any one faced this issue please share some idea.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sushri
  • 295
  • 1
  • 3
  • 12

4 Answers4

16

C#

string IPAddress = GetIPAddress();

public string GetIPAddress()
{
    IPHostEntry Host = default(IPHostEntry);
    string Hostname = null;
    Hostname = System.Environment.MachineName;
    Host = Dns.GetHostEntry(Hostname);
    foreach (IPAddress IP in Host.AddressList) {
        if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
            IPAddress = Convert.ToString(IP);
        }
    }
    return IPAddress;
}

VB.net

Dim Host As IPHostEntry
Dim Hostname As String
Hostname = My.Computer.Name
Host = Dns.GetHostEntry(Hostname)
For Each IP As IPAddress In Host.AddressList
    If IP.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
        IPAddress = Convert.ToString(IP)
    End If
    Next
Return IPAddress

Hope this helps

leocabrallce
  • 163
  • 1
  • 8
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
  • Thanks for your time Amarnath. But I am facing some issue at this line: Hostname = My.Computer.Name; Is there any other dependencies. – Sushri Jan 16 '14 at 07:33
  • My.Computer.Name is not recognized. Do we need to add anything else added in code. – Sushri Jan 16 '14 at 09:45
  • 2
    @Sushri i have updated my answer Hostname = System.Environment.MachineName; – Amarnath Balasubramanian Jan 16 '14 at 10:00
  • 17
    This is returning the server's IP address, not the client's IP address as asked in the question. The OP is trying to pull the IP address from the current `HttpContext`, but you're getting it from the server. This doesn't answer the question (though it may very nicely answer a different one!). – decates Oct 04 '18 at 11:08
10
private string GetUserIP()
 {
     return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];    
 }

You may get several ip address, so can split them as-

private string GetUserIP()
    {
        string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipList))
        {
            return ipList.Split(',')[0];
        }

        return Request.ServerVariables["REMOTE_ADDR"];
    }
Ramashankar
  • 1,598
  • 10
  • 14
  • Keep in mind that the HTTP_X_FORWARDED_FOR header can contain multiple IP's separated by comma. Normally, the correct IP is the last one. This is because of proxies that add their own IP to the "chain". – Pascal Mathys Jan 16 '14 at 07:11
  • Nice. This seems to be the only answer that actually addresses the question: getting the CLIENT address for the current request (not the server address). – decates Oct 04 '18 at 11:10
7

try using this

string ip=System.Net.Dns.GetHostEntry
               (System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
  • Thanks Ronit... this code wored for getting the IPv6 address and I modifed it a bit to get the IPv4 address. The code to get the IPv4 address is : string ip=System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName()).AddressList.GetValue(1).ToString(); – Sushri Jan 16 '14 at 07:44
6

In my project it's required to get the local PC IP. So i use it Please try the below code

string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string ip = addr[1].ToString();
Sapnandu
  • 620
  • 7
  • 9