21

I am running an ASP.NET MVC app in the localhost - dev server given with a Visual Studio. I want to get the IP address. I tried

Request.UserHostAddress

and

Request.ServerVariables("REMOTE_ADDR")

In both cases, I am getting::1 as a result. What is it? Why am I getting it? How can I get 127.0.0.1 or 192.168.1.xxx?

1

6 Answers 6

22

You are getting a valid IP Address; ::1 is localhost for IPv6.

4
  • 2
    Well okay.. so how can I get the IPv4 IP?
    – user529141
    Commented Apr 26, 2011 at 19:47
  • 2
    OH wow! Is this working this way only because its the same machine - localhost. Will I get IPv6 address also when I deploy to prod server? I don't want the client's IPv6 address, I want their IPv4 address.. how can I get them?
    – user529141
    Commented Apr 26, 2011 at 19:50
  • 2
    Here's a good example. Though the final bit showing usage needs to be changed from IP4.GetIP4Address to IPNetworking.GetIP4Address() (I use () even on parameterless methods in VB.NET, but the class name is the key)
    – pickypg
    Commented Apr 26, 2011 at 20:30
  • 1
    but i want proper IP address? i don't want to like ::1
    – user7918630
    Commented Oct 27, 2017 at 13:11
16

What you're seeing when calling 'localhost' is valid. ::1 is the IPv6 loopback address. Equivalent to 127.0.0.1 for IPv4.

Instead of calling:

 http://localhost/...

Call:

http://{machinename}/...

or

http://127.0.0.1/...

or

http://192.168.1.XXX/...

[Replace {machinename} with your machine's computer name. Replace XXX with your computer's IP address.]

Anyone calling into your machine to the MVC app will have their valid IP address as a result. If the client is an IPv6 host it will save their IPv6 IP address. If the client is an IPv4 host it will save their IPv4 IP address.

If you always want to save an IPv4 address take a look at this article on how they accomplished it with a simple class https://web.archive.org/web/20211020102847/https://www.4guysfromrolla.com/articles/071807-1.aspx. You should be able to take their example and build a quick helper method to accomplish this.

2
  • Well.. I replaced localhost with 127.0.0.1 and it showed that. By machine name didn't work. I get "Unable to connect" error..
    – user529141
    Commented Apr 26, 2011 at 19:44
  • I also try this but the solution is same e I get::1
    – user7918630
    Commented Oct 27, 2017 at 13:17
1
Request.Params["REMOTE_ADDR"]

instead of Request.ServerVariables("REMOTE_ADDR")

1
  • Yes. I looked into this. Basically what is happening is, your computer is using the new IP system. Localhost in IPv6 is ::1 - social.msdn.microsoft.com/forums/en-US/wcf/thread/…. If you access from an Ipv4 client, you will get the IPv4 address instead.
    – slandau
    Commented Apr 26, 2011 at 20:01
1

If you want localhost return 127.0.0.1, maybe you need to change your "hosts" file. You can find it in "%systemdrive%\Windows\System32\drivers\etc"

It works for me, now I get 127.0.0.1 with "Request.ServerVariables["REMOTE_ADDR"]". I uncomment 127.0.0.1 (remove #).

Here you can find default hosts file http://support.microsoft.com/kb/972034

My file

# localhost name resolution is handled within DNS itself.

127.0.0.1       localhost

# ::1 localhost

1
  • but i want proper IP address don like ::1? Please anyone help me
    – user7918630
    Commented Oct 27, 2017 at 13:16
0

below code i have used for finding ip

public static string GetIp()
        {
            var Request = HttpContext.Current.Request;

            try
            {

                Console.WriteLine(string.Join("|", new List<object> {
                    Request.UserHostAddress,
                    Request.Headers["X-Forwarded-For"],
                    Request.Headers["REMOTE_ADDR"]
                })
                );

                var ip = Request.UserHostAddress;
                if (Request.Headers["X-Forwarded-For"] != null)
                {
                    ip = Request.Headers["X-Forwarded-For"];
                    Console.WriteLine(ip + "|X-Forwarded-For");
                }
                else if (Request.Headers["REMOTE_ADDR"] != null)
                {
                    ip = Request.Headers["REMOTE_ADDR"];
                    Console.WriteLine(ip + "|REMOTE_ADDR");
                }
                return ip;
            }
            catch (Exception ex)
            {
                Log.WriteInfo("Message :" + ex.Message + "<br/>" + Environment.NewLine +
                    "StackTrace :" + ex.StackTrace);
            }
            return null;
        }
0

If you get ipv6 address , after that you can find it valid ipv4 address map for ipv6 address.

c # code is below;

  public static string GetIP4Address(string ip) {
        try {
            var hostNames = Dns.GetHostEntry(ip);
            var ipv4 = hostNames.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
            if(ipv4 != null) {
                return ipv4.ToString();
            }

        } catch(Exception ex) {
            log.WarnFormat("Error When Getting Client Ipv4");
        }

        return ip;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.