Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ipaddress and location of every user visiting your website

How do you get the ipaddress and location of every website vistor of your website through Asp.Net?

Thanks

like image 762
Josh Avatar asked Apr 28 '09 15:04

Josh


3 Answers

To get the user's IP use:

Request.UserHostAddress

You can use this webservice to get their geographic location. http://iplocationtools.com/ip_location_api.php

like image 98
Nathan Koop Avatar answered Sep 22 '22 21:09

Nathan Koop


 string VisitorIPAddress = Request.UserHostAddress.ToString();

and based on the ipaddress you can narrow down the location: find the geographical location of a host

like image 24
TStamper Avatar answered Sep 20 '22 21:09

TStamper


Request.UserHostAddress won’t work if you're behind a proxy. Use this code:

public static String GetIPAddress()
{
    String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ip))
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    else
        ip = ip.Split(',')[0];

    return ip;
}

Note that HTTP_X_FORWARDED_FOR should be used BUT as it can return multiple IP addresses separated by a comma you need to use the Split function. See this page for more info.

like image 43
Anthony Avatar answered Sep 19 '22 21:09

Anthony