简体   繁体   English

无法在我发布的网站上获取客户端的IP地址

[英]Cant Get the IP Address Of the client On my Published Website

I have implemented a simple API that returns the ip address of the client "Request Sender". 我实现了一个简单的API,该API返回客户端“请求发送者”的IP地址。 When I'm outside the network it always returns the same ip address when inside the network it works perfectly fine and it returns the local ip address of the device. 当我不在网络中时,它总是返回相同的IP地址,而在网络内部时,它工作得很好,并且返回设备的本地IP地址。 The website is hosted on IIS the api logic is the following: 该网站托管在IIS上,其api逻辑如下:

public static class iphelper
    {
        private const string HttpContext = "MS_HttpContext";
        private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";

        public static string GetClientIpAddress(this HttpRequestMessage request)
        {
            if (request.Properties.ContainsKey(HttpContext))
            {
                dynamic ctx = request.Properties[HttpContext];
                if (ctx != null)
                {
                    return ctx.Request.UserHostAddress;
                }
            }

            if (request.Properties.ContainsKey(RemoteEndpointMessage))
            {
                dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
                if (remoteEndpoint != null)
                {
                    return remoteEndpoint.Address;
                }
            }

            return null;
        }
    }
}

I have tried ALL of the Snippets out there all returns the same. 我已经尝试了所有的代码片段都返回相同的。 My project uses ASP.NET MVC C#. 我的项目使用ASP.NET MVC C#。

i tried HTTP_X_FORWARDED_FOR always return null 我尝试过HTTP_X_FORWARDED_FOR总是返回null

Most likely it is related to a load balancer or network device in front of your web server. 它很可能与Web服务器前的负载平衡器或网络设备有关

Given that: 鉴于:

REMOTE_ADDR reruns 192.168.130.140 REMOTE_ADDR重新运行192.168.130.140

I would suggest asking your Operations team what machine is running at that IP, and how it could pass through the calling IP Address (likely through X-Forwarded-For ). 我建议您咨询您的运营团队,该IP运行的是哪台计算机,以及如何通过调用IP地址(可能通过X-Forwarded-For )进行传递。

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

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