get IP from the machine in asp.net c #

3

Good I'm wanting to get the ip of the machine.

For that I'm doing this code that when I run it from my website and publish it in my local IIS it works and I get my IP from my machine.

   IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

 String IP = Convert.ToString(localIPs[1]);

This is the url of my page that at the moment of calculating you should get the ip of the machine that is doing the test.

but the moment I publish it on an external website, it means that it is entered on the internet.

I see that the IP of the IIS is obtained and not the IP of the user.

I hope you can help me.

/ ****************** Rspta by Luciano Montañez ************************ ****** /

Place this code.

String IP = Request.UserHostAddress;
    
asked by PieroDev 25.07.2018 в 17:26
source

2 answers

3

Inside a controller, when you receive a request.- This comes within object HttpContext.Request

string IP = Request.UserHostAddress;

Note: Obtaining "::1" as a response is not an error, but it is returned as you do request/response from the same localhost

    
answered by 25.07.2018 / 17:40
source
0
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());// objeto para guardar la ip
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();// esta es nuestra ip
                }
            }
    
answered by 28.07.2018 в 16:25