vb.net 为什么我在 ASP.Net 中得到 ::1 作为 IP 地址......以及如何获得正确的 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5795534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting ::1 as IP address in ASP.Net.. and how to get the proper IP address?
提问by pickypg
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
我正在本地主机中运行一个 ASp.Net MVC 应用程序 - 一个 Visual Studio 提供的开发服务器。我想获取IP地址。我试过
Request.UserHostAddress
Request.UserHostAddress
and
和
Request.ServerVariables("REMOTE_ADDR")
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 结果。它是什么?为什么我得到它?如何获得 127.0.0.1 或 192.168.1.xxx?
回答by pickypg
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)
您正在获得一个有效的 IP 地址。::1
是 IPv6 中的 local_host。(在 local_host 中使用下划线来阻止 SO 认为它是某种不好的文本)
回答by Mark
What you're seeing when calling 'localhost' is valid. ::1 is the IPv6 loopback address. Equivalent to 127.0.0.1 for IPv4.
您在调用 'localhost' 时看到的内容是有效的。::1 是 IPv6 环回地址。相当于 IPv4 的 127.0.0.1。
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 XXXwith your computer's IP address.]
[将{machinename}替换为您机器的计算机名称。将XXX替换为您计算机的 IP 地址。]
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.
因此,任何人通过您的机器调用 MVC 应用程序都将获得其有效的 IP 地址。如果客户端是 IPv6 主机,它将保存 IPv6 IP 地址。如果客户端是 IPv4 主机,它将在那里保存 IPv4 IP 地址。
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.
如果您总是想保存 IPv4 地址,请查看这篇文章,了解他们如何通过一个简单的类http://www.4guysfromrolla.com/articles/071807-1.aspx来实现它。您应该能够以那里为例并构建一个快速帮助方法来完成此操作。
回答by slandau
Request.Params["REMOTE_ADDR"]
instead of Request.ServerVariables("REMOTE_ADDR")
代替 Request.ServerVariables("REMOTE_ADDR")
回答by Dipa
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"
如果您希望 localhost 返回 127.0.0.1,也许您需要更改“hosts”文件。您可以在“%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 #).
它对我有用,现在我用“Request.ServerVariables[“REMOTE_ADDR”]”得到 127.0.0.1。我取消注释 127.0.0.1(删除 #)。
Here you can find default hosts file http://support.microsoft.com/kb/972034
在这里您可以找到默认主机文件 http://support.microsoft.com/kb/972034
My file
我的档案
# localhost name resolution is handled within DNS itself.
# 本地主机名称解析在 DNS 本身内处理。
127.0.0.1 localhost
# ::1 localhost
# ::1 本地主机
回答by Andi AR
below code i have used for finding ip
下面是我用来查找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;
}
回答by Mert Cengiz
If you get ipv6 address , after that you can find it valid ipv4 address map for ipv6 address.
如果您获得 ipv6 地址,之后您可以找到有效的 ipv6 地址的 ipv4 地址映射。
c # code is below;
c#代码如下;
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;
}