简体   繁体   English

在C#中获取IP地址

[英]Getting IP Address in C#

I currently have a RESTful C# web service that gets the client's IP address. 我目前有一个RESTful C#Web服务,该服务获取客户端的IP地址。 This was working fine until load-balancing was implemented. 在实施负载平衡之前,它一直运行良好。 So now the web service is sitting behind a load balancer. 因此,现在Web服务位于负载平衡器的后面。

Now, every time I try to get the user's IP address, I get the IP address of the load balancer instead. 现在,每次我尝试获取用户的IP地址时,我都会获取负载均衡器的IP地址。 Here is what the code looks like... 这是代码的样子……

System.Web.HttpContext context = System.Web.HttpContext.Current;
        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_CLIENT_IP") && CheckIP(context.Request.ServerVariables["HTTP_CLIENT_IP"]))
            return new Model(context.Request.ServerVariables["HTTP_CLIENT_IP"]);

        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED_FOR"))
            foreach (string ip in context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(','))
                if (CheckIP(ip.Trim()))
                    return new Model(ip.Trim());

        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_X_FORWARDED"]))
            return new Model(context.Request.ServerVariables["HTTP_X_FORWARDED"]);

        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_CLUSTER_CLIENT_IP") && CheckIP(context.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]))
            return new Model(context.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]);

        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_FORWARDED_FOR") && CheckIP(context.Request.ServerVariables["HTTP_FORWARDED_FOR"]))
            return new Model(context.Request.ServerVariables["HTTP_FORWARDED_FOR"]);

        if (context.Request.ServerVariables.AllKeys.Contains("HTTP_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_FORWARDED"]))
            return new Model(context.Request.ServerVariables["HTTP_FORWARDED"]);

        return new Model(context.Request.ServerVariables["REMOTE_ADDR"]);

Despite this, I am still ultimately getting the wrong IP address. 尽管如此,我仍然最终得到错误的IP地址。 Any thoughts? 有什么想法吗? Ideas? 想法? Am I missing something? 我想念什么吗?

A well-behaved load balancer should place one or more IP addresses in the X_FORWARDED_FOR header. 行为良好的负载均衡器应在X_FORWARDED_FOR标头中放置一个或多个 IP地址。 Your code is checking HTTP_FORWARDED_FOR and two other variables that aren't quite right. 您的代码正在检查HTTP_FORWARDED_FOR和另外两个不太正确的变量。

Not all load-balancers are well behaved, and some can be configured to include that header if it is not currently in there. 并非所有的负载均衡器都表现良好,并且如果当前未在其中,则可以将某些负载均衡器配置为包括该头。

If there are multiple forwarding points, there will be multiple IP addresses in the header. 如果有多个转发点,则标头中将有多个IP地址。 In that case, the first one listed is the client IP 在这种情况下,列出的第一个是客户端IP

X-Forwarded-For: client, proxy1, proxy2 X-Forwarded-For:客户端,代理1,代理2

        string sIpaddress = "";
        if (Request.Headers["True-Client-IP"] != null)
        {
            sIpaddress = Request.Headers["True-Client-IP"]; //if the user is behind a proxy server
        }
        if (sIpaddress == "")
        {
            if (Request.Headers["CF-CONNECTING-IP"] != null)
                sIpaddress = Request.Headers["CF-CONNECTING-IP"];
        }
        if (sIpaddress == "")
        {
            if (Request.Headers["X-Forwarded-For"] != null)
                sIpaddress = Request.Headers["X-Forwarded-For"];
        }
        if (sIpaddress == "")
        {
            if (Request.ServerVariables["REMOTE_ADDR"] != null)
                sIpaddress = Request.ServerVariables["REMOTE_ADDR"];
        }

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

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