Why am I getting ::1 as IP address in ASP.Net.. an

2020-02-10 12:49发布

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?

5条回答
劫难
2楼-- · 2020-02-10 13:13
Request.Params["REMOTE_ADDR"]

instead of Request.ServerVariables("REMOTE_ADDR")

查看更多
来,给爷笑一个
3楼-- · 2020-02-10 13:14

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

查看更多
The star\"
4楼-- · 2020-02-10 13:15

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 there IPv6 IP address. If the client is an IPv4 host it will save there 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 http://www.4guysfromrolla.com/articles/071807-1.aspx. You should be able to take there example and build a quick helper method to accomplish this.

查看更多
我想做一个坏孩纸
5楼-- · 2020-02-10 13:26

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;
        }
查看更多
别忘想泡老子
6楼-- · 2020-02-10 13:34

You are getting a valid IP Address. ::1 is local_host in IPv6. (underscore used in local_host to stop SO from thinking it was some sort of bad text)

查看更多
登录 后发表回答