在asp.net中获取客户端计算机的公共IP地址 [英] Get the public ip address of a client machine in asp.net

查看:68
本文介绍了在asp.net中获取客户端计算机的公共IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI


如何使用asp.net获取客户端计算机的公共IP地址。我已经测试了许多站点中给出的许多代码,但它只显示了客户端机器的IP地址,该地址是在局域网中给出的,而不是真正的公共IP地址。



谢谢

HI
how to get the public ip address of a client machine using asp.net. I have tested many code which are given in many sites, but it shows only the ip address of client machine which is given in a LAN but not the real public ip address.

thanks

推荐答案

试试这个代码..



try this code..

protected void GetUser_IP()
{
    string VisitorsIPAddr = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
    }
    uip.Text = "Your IP is: " + VisitorsIPAddr;
}





更新



查看此链接



获取外部IP地址 [ ^ ]


http://stackoverflow.com/questions/19285957/how -to-get-public-ip-address-of-a-user-in-c-sharp
http://stackoverflow.com/questions/19285957/how-to-get-public-ip-address-of-a-user-in-c-sharp


您将只能获取公共IP地址而不能获取其本地地址。 (防火墙是出于这个原因)



至于获取计算机的私有IP和名称,你也无法通过ASP.NET实现这一点。您需要在用户计算机上删除ActiveX控件或某种其他应用程序才能执行此操作,大多数人都不允许这样做。



对于访问应用程序的人来说,最好的办法是在应用程序中实现某种成员身份并跟踪他们登录应用程序本身时的情况
You will only be able to get the public IP address and not their local address. (firewalls are made for this reason)

As far as getting a computer's private IP and name you can not do this via ASP.NET either. You would need to drop an ActiveX control or some sort of other application on the users machine to do so which most anyone would not allow as you have pointed out.

When it comes to who accessed the application your best bet would be to implement some sort of membership into your application and track when they log into the application itself


这篇关于在asp.net中获取客户端计算机的公共IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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