简体   繁体   English

客户端IP地址返回相同的内部网络地址

[英]Client IP address returns the same internal network address

I'm trying to get the user's IP address from ASP.NET MVC 5. I've looked up various examples, such as these: 我正在尝试从ASP.NET MVC 5获取用户的IP地址。我已经查找了各种示例,例如:

They've all produced the same result: the user is considered internal to the network. 它们都产生了相同的结果:用户被认为是网络的内部。 I've had friends try their phones (which are not on the network). 我有朋友试试他们的手机(不在网络上)。 Here's my latest attempt: 这是我最近的尝试:

private static Logger _logger = LogManager.GetCurrentClassLogger();
public static bool IsIpInternal()
{
    var ipAddress = HttpContext.Current.Request.UserHostAddress;
    var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, ipAddress);
    _logger.Log(logEvent);
    try
    {
        if (ipAddress != null)
        {
            var ipParts = ipAddress.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse).ToArray();
            var isDebug = System.Diagnostics.Debugger.IsAttached;

            if (ipParts[0] == 10)
            {
                return true;
            }
        }
    }
    catch (Exception e)
    {
        logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, e.Message);
        _logger.Log(logEvent);
        return false;
    }
    return false;
}

The log is showing 10.xxx.xx.xxx for all requests (based on the log). 对于所有请求,日志显示10.xxx.xx.xxx (基于日志)。 This is an internal address rather than the IP of the client connecting to the web app. 这是一个内部地址,而不是连接到Web应用程序的客户端的IP。 The IsIpInternal() returns true always. IsIpInternal()始终返回true。 What am I doing wrong? 我究竟做错了什么?

Note that I'm ignoring 192.168.xx and 172.16.xxx.xxx addresses as being internal. 请注意,我忽略了192.168.xx172.16.xxx.xxx地址作为内部。

If your web site is behind a load balancer, it is a common problem for the load balancer's IP address to appear when you are expecting the client's IP address. 如果您的网站位于负载均衡器后面,那么当您需要客户端的IP地址时,负载均衡器的IP地址就会出现一个常见问题。 That is because in reality the load balancer is the only client that the web application knows is talking to it. 这是因为实际上负载均衡器是Web应用程序知道与之通信的唯一客户端。

There are two ways to deal with this: 有两种方法可以解决这个问题:

  1. You can configure the load balancer to add an additional HTTP header ( x-forwarded-for ) that specifies the original IP address. 您可以配置负载平衡器以添加指定原始IP地址的其他HTTP标头( x-forwarded-for )。 You will need to modify your web site to look at this header instead of the UserHostAddress, like this: 您需要修改您的网站以查看此标头而不是UserHostAddress,如下所示:

     //var clientIP = HttpContext.Current.Request.UserHostAddress; var clientIP = HttpContext.Current.Request.Headers["x-forwarded-for"]; 

    Note: The x-forwarded-for header can actually return a comma-delimited list of IP addresses in some cases. 注意:在某些情况下,x-forwarded-for标头实际上可以返回以逗号分隔的IP地址列表 So to be compatible with such an occurence, you might write this instead: 因此,要与这样的事件兼容,您可以改为:

     var clientIP = HttpContext.Current.Request.Headers["x-forwarded-for"].Split(',')[0]; 
  2. You can configure certain LBs to pass through the client IP by copying the IP header packet. 您可以通过复制IP标头数据包来配置某些LB以通过客户端IP。 For Citrix Netscaler, see this article for more information. 对于Citrix Netscaler,请参阅此文章以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM