# .NET and More > ASP.NET And ASP.NET Core >  Just to be sure, get remote IP address

## sapator

Hi.
I'm asking to be sure.
I do this to get a PC address on forms:


```
 public static String GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if(ip.AddressFamily == AddressFamily.InterNetwork)
                {
                return ip.ToString();
            }
        }

        return "";
    }
```

Now this will not work on internet get's but I've read that it will work with Local networks. Is that the case? Meaning I can get an address of a PC that is on the network using a web application or I will just get the server IIS name?

Thanks.

----------


## PlausiblyDamp

> Hi.
> I'm asking to be sure.
> I do this to get a PC address on forms:
> 
> 
> ```
>  public static String GetLocalIPAddress()
>     {
>         var host = Dns.GetHostEntry(Dns.GetHostName());
> ...


Dns.GetHostName will return whatever IPAddresses are assigned to the local machine, these addresses are normally local to the network and not directly reachable over the internet. Your code will actually only return the 1st address associated with the local machine, rather than all of them the following might be better as it returns all of them


```
static IEnumerable<String> GetLocalIPAddress()
{
     var host = Dns.GetHostEntry(Dns.GetHostName());
     foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
        yield return ip.ToString();
        }
    }
}
```

If you are trying to get the external address of you local machine you might be better making an http request to something like http://checkip.dyndns.org/ as this will report on your public address (typically your public router or firewall). Anything on the internet could then reach that, although there is a good chance you would then need to configure the public firewall / router / whatever to allow connections in to your actual pc.

----------


## sapator

Hey.
I'm actually trying to get whatever machine local address the PC is using. So mine is 192.168.1.1 when a person on another building on the local network hits the site it will be 192.168.20.1 etc.
So simulate a whatismyip thing. I'm thinking it might be able to be done with javascript but from what I've read the browsers might or might not support it.

From what I'm reading I believe something more viable would be this: https://www.c-sharpcorner.com/blogs/...ress-in-mvc-30
Will have to try it out tho and since next week I'm working from home, err, it will take a while.


Edit: Tested it locally and it returns ::1  , so I don't have a clue on what will return remotely.

----------


## danasegarane

This will return the server ip address where the application is running not the client IP address. You can try something like



```
string clientIp = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? 
                   Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
```

This will get the details from browser. Also you can try printing the values of Request.ServerVariables and see if that has more useful values

----------


## vikas_jk

This should help



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

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

        return Request.ServerVariables["REMOTE_ADDR"];
    }
```

----------

