Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
1.81/5 (4 votes)
See more:
I want to find the IP address of logged in user of website. how to find it?
Posted

You can't find this out. The best you can do is

C#
Request.UserHostAddress


however that isn't guaranteed to be the user's address, if they are going through a proxy etc it could be the address of that.
 
Share this answer
 
I know of 2 ways; which I use. To obtain the IP Address of the router if it is a transparent non-anonymous use Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. Secondly, you can use the Request.UserHostAddress if the router is a non-anonymous, non-transparent. Sometimes these values are a bit different, but most of the time they're the same. If the client comes from an anonomous proxy, then you won't be able to get the IP Address.

C#
string userHostAddress = Request.UserHostAddress;
string ipAddress = GetIPAddress();

protected string GetIPAddress()
{
    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"];
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900