简体   繁体   English

获取用户IP地址

[英]Get user IP Address

I have a web application that has a login. 我有一个具有登录名的Web应用程序。 Now what I want is to get the IP address of that end user upon login. 现在,我想要的是在登录时获取该最终用户的IP地址。 I've tried researching but based on what I understood, it gets the ip of my machine not the person that logins. 我曾尝试进行研究,但根据我的理解,它获取我机器的IP,而不是登录用户的IP。 This app is already deployed on a server. 此应用程序已部署在服务器上。 And if I do what they said, I will be getting the server's ip address. 如果我按照他们说的去做,我将获得服务器的IP地址。 But I want the IP address of the end user that logs in. 但是我想要登录的最终用户的IP地址。

This is a sample of what I've seen. 这是我所见的示例。

private string GetIP()
{
    string strHostName = "";
    strHostName = System.Net.Dns.GetHostName();

    IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

    string ipaddress =convert.tostring(ipEntry.AddressList[2]);

    return ipaddress.tostring();

}

I'm not sure if I understood this correctly but I think this will get local ip address, not the ip address of the end user that is logging in. Please do correct me if I'm wrong. 我不确定我是否理解正确,但是我认为这将获得本地IP地址,而不是登录的最终用户的IP地址。如果我输入错了,请更正我。 Any ideas? 有任何想法吗? Thanks! 谢谢!

采用 -

HttpContext.Current.Request.UserHostAddress

Try both : 同时尝试:

HttpContext.Current.Request.UserHostAddress;

or 要么

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

you can use REMOTE_ADDR and HTTP_X_FORWARDED_FOR from Request.ServerVariables . 您可以从Request.ServerVariables使用REMOTE_ADDR和HTTP_X_FORWARDED_FOR。

The UserHostAddress property is simply a wrapper around the REMOTE_ADDR server variable. UserHostAddress属性只是REMOTE_ADDR服务器变量的包装。 So if you user is behind a proxy (or router), this returns only one IP Address, the IP Address of the proxy (or router) . 因此,如果用户位于代理(或路由器)的后面,则此操作仅返回一个IP地址,即代理(或路由器)的IP地址。

Red : http://haacked.com/archive/2006/10/11/A_Gotcha_Identifying_the_Users_IP_Address.aspx/ 红色: http : //haacked.com/archive/2006/10/11/A_Gotcha_Identifying_the_Users_IP_Address.aspx/

There is another thread in stackoverflow : How to Get IP Address? stackoverflow中还有另一个线程: 如何获取IP地址?

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

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