在 Localhost 上寻找外部 IP 地址

Looking for External IP Address on Localhost

您好,我正在学校的一个安全网站上登录,但它都是基于本地主机的。

我希望真正捕获访问我网站的人。我实际上想捕获此人使用的任何机器的外部 IP 地址。例如,当我 运行 我的网站并点击一个按钮时,它会立即捕获我的外部 ip 地址。

坦率地说,我对 ip 地址这方面的知识不多,但我对客户端、服务器、ip 地址、端口号等是什么有足够的了解,这些是我在网络模块中学到的.

要提供更多信息,当您访问 website to check IP address 或任何其他您的 ip 网站时,您将获得您的外部 ip 地址。我其实想捕获这个 IP 地址并将其保存到我的数据库中。

我确实对 Whosebug、codeproject 进行了研究,无论我能从中找到什么帮助,但它总是 return ::1,我认为它是 127.0.0.1 我们的默认本地主机地址?

我已经尝试了下面的所有这些来试验它们生产的东西..

protected void getIP_Click(object sender, EventArgs e)
{
    String hostName = System.Net.Dns.GetHostName();
    String clientIpAddressHostName = System.Net.Dns.GetHostAddresses(hostName).GetValue(0).ToString();
    IP1.Text = clientIpAddressHostName;

    String clientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"].ToString();
    IP2.Text = clientIpAddress;

    String ip = null;

    if (String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
    {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    IP3.Text = ip;

    String ipNext = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    IP4.Text = ipNext;

    String ipNextNext = HttpContext.Current.Request.UserHostAddress;
    IP5.Text = ipNextNext;

    String last = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
    IP6.Text = last;
}

我还在网上看到,我实际上可以根据下面来自 Whosebug 的答案从网站的外部来源获取我的外部 IP 地址

Here

但我还没有尝试过,因为我认为这不是一种安全的做法?如有错误请指正..

高低搜索后仍然无法得到我要找的东西..

有人可以帮忙吗?非常感谢任何帮助!

您似乎正在读取您的服务器 IP 地址。

可以通过以下方式读取客户端 IP 地址:Http​Request.​User​Host​Address

您在服务器中收到的 IP 地址将是客户端的“public”地址。即,如果用户在代理后面,您将获得代理地址。

如果您使用 IIS 托管您的网站,您可以将其配置为记录客户端 IP 地址。在这种情况下,您无需编写任何代码来记录 IP 地址。

Configure IIS Logging

In the W3C Logging Fields dialog box, select one or more of the following options:

Client IP Address (c-ip): the IP address of the client that made the request.