C# 如何获取IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1907195/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:46:15  来源:igfitidea点击:

How to Get IP Address?

c#.netasp.net

提问by Developer404

I want to get the ip address whoever is registering in my site. How to do this in ASPNET. I used the following code, but, it is not getting the proper IP Address

我想获得在我网站上注册的人的 IP 地址。如何在 ASPNET 中执行此操作。我使用了以下代码,但是没有获得正确的 IP 地址

string ipaddress = Request.UserHostAddress;

采纳答案by Muhammad Akhtar

You can use this method to get the IP address of the client machine.

您可以使用此方法获取客户端机器的 IP 地址。

public static String GetIP()
{
    String ip = 
        HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

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

    return ip;
}

回答by RickNZ

If a client is connecting through a transparent non-anonymous proxy, you can get their address from:

如果客户端通过透明的非匿名代理连接,您可以从以下位置获取他们的地址:

Request.ServerVariables["HTTP_X_FORWARDED_FOR"]

which can return null or "unknown" if the IP can't be obtained that way.

如果无法通过这种方式获取 IP,则可以返回 null 或“未知”。

Request.ServerVariables["REMOTE_ADDR"]should be the same as Request.UserHostAddress, either of which can be used if the request is not from a non-anonymous proxy.

Request.ServerVariables["REMOTE_ADDR"]应该与 相同Request.UserHostAddress,如果请求不是来自非匿名代理,则可以使用任何一个。

However, if the request comes from an anonymous proxy, then it's not possible to directly obtain the IP of the client. That's why they call those proxies anonymous.

但是,如果请求来自匿名代理,则无法直接获取客户端的 IP。这就是为什么他们称这些代理为匿名的

回答by Anthony

HTTP_X_FORWARDED_FOR should be used BUT it can return multiple IP addresses separated by a comma. See this page.

应该使用 HTTP_X_FORWARDED_FOR 但它可以返回多个以逗号分隔的 IP 地址。请参阅此页面

So you should always check it. I personally use the Split function.

所以你应该经常检查它。我个人使用拆分功能。

public static String GetIPAddress()
{
    String ip = 
        HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (string.IsNullOrEmpty(ip))
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    else
        ip = ip.Split(',')[0];

    return ip;
}

回答by Wouter Simons

In a situation where you use the IP address for security you should be aware of your infrastructure.

在将 IP 地址用于安全的情况下,您应该了解您的基础设施。

If you are using a proxy between your web server and your clients that sets the header, you should be able to trust the last address. Then you use the code like Muhammed suggested with an update to always get the last IP address from the forward header (See code below)

如果您在 Web 服务器和设置标头的客户端之间使用代理,您应该能够信任最后一个地址。然后你使用像 Muhammed 建议的代码更新总是从转发头中获取最后一个 IP 地址(见下面的代码)

If you do not use a proxy, beware that the X-Forwarded-For header is very easy to spoof. I suggest you ignore it then unless you have a clear reason why not to.

如果您不使用代理,请注意 X-Forwarded-For 标头很容易被欺骗。我建议你忽略它,除非你有明确的理由不这样做。

I updated Muhammed Akhtar's code as follows to allow you to choose:

我更新了 Muhammed Akhtar 的代码如下,让你选择:

public string GetIP(bool CheckForward = false)
{
    string ip = null;
    if (CheckForward) {
        ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    }

    if (string.IsNullOrEmpty(ip)) {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    } else { // Using X-Forwarded-For last address
        ip = ip.Split(',')
               .Last()
               .Trim();
    }

    return ip;
}

This Wikipedia articleexplains the risks more thoroughly.

这篇维基百科文章更彻底地解释了风险。

回答by Krishna Kumar

string result = string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

回答by Erwin

In MVC 6, you retrieve the IP address this way:

在 MVC 6 中,您可以通过以下方式检索 IP 地址:

HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString()