如何获取客户端的本地IP如192.168.0.1 [英] How Can I Get Client's Local Ip Like 192.168.0.1

查看:210
本文介绍了如何获取客户端的本地IP如192.168.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i需要获取客户端的本地IP地址(局域网),当我尝试这样做总是给我服务器的万维网



这是我的代码



Hi
i need to get client's local ip adress (LAN) when i try to do it always gives me server's wan ip

here is my code

protected string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}





i尝试了LOCAL_ADDR,但仍然相同,它给服务器带来了好消息



是否有人为此设置了孤子。



i tried LOCAL_ADDR but still same it gives server wan ip

does anyone have a soliton for this.

推荐答案

您只能获得发出请求的机器的IP,可能是也可能不是实际的客户。如果客户端通过代理访问您的站点,那么您将获得代理地址。你无能为力。谷歌asp.net获取客户端IP,你会发现这是一个非常常见的问题。
You can only get the IP of the machine that made the request, that may or may not be the actual client. If the client accesses your site via a proxy then you'll get the proxy address. There is nothing you can do about this. Google "asp.net get client ip" and you'll find this is a very frequently asked question.


你几乎不能 - 而且它不会被大量使用无论如何,因为几乎所有局域网都使用192.xyz寻址。您无法使用它来访问客户端,因为它将被假定为您服务器LAN中的本地地址。



并且客户端IP不会出现任何问题不管怎么说,路由器远远超过路由器!



从理论上讲,你可以在非常非常专业的情况下得到它,但它们不会发生在绝大多数用户:客户端必须运行IE,他必须接受ActiveX控件,他必须允许ActiveX控件运行,并且必须编写ActiveX控件。 IE不是世界上最受欢迎的浏览器,默认情况下禁用所有ActiveX控件,因为它们是一个巨大的安全风险!
You pretty much can't - and it wouldn't be a lot of use to you anyway, as nearly all LANs use 192.x.y.z addressing. You can't use it to access the client, because it will be assumed to be a local address within your server LAN.

And client IP's don't go any further than the router anyway for pretty much just that reason!

In theory, you could get it under very, very specialised circumstances, but they don;t occur for the vast majority of users: The client must be running IE, he must accept ActiveX controls, he must permit ActiveX controls to run, and you must write an ActiveX control. IE isn't the worlds' most popular browser, and all ActiveX controls are disabled by default as they are a HUGE security risk!


这篇关于如何获取客户端的本地IP如192.168.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆