且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在asp.net中获取客户端计算机的公共IP地址

更新时间:2022-05-08 22:29:56

试试这个代码..



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://***.com/questions/19285957/how -to-get-public-ip-address-of-a-user-in-c-sharp
http://***.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