I'm trying to get a user's country based on IP address in web api. Using userhostaddress it returns a Private IP not their public IP.
This is what I've been trying:
string clientAddress = HttpContext.Current.Request.UserHostAddress;
string nowip = null;
nowip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(nowip)){
nowip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
WebRequest request = WebRequest.Create("http://checkip.dyndns.org");
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string ipAddress = stream.ReadToEnd();