Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain user's current visiting country location in .NET MVC?

Tags:

asp.net-mvc

I have gone through so many documents but nothing is working out for me. I have followed a link

I want to get current user's location by IP address. But, my ipAddress always shows null and then ::1. I do not know what am I doing wrong.

Anyone can please help me to overcome this problem.

Model

   public class LocationModel
    {
        public string IP { get; set; }
        public string Country_Code { get; set; }           

    }

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }
        LocationModel location = new LocationModel();
        string url = string.Format("http://freegeoip.net/json/{0}", ipAddress);
        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);
            location = new JavaScriptSerializer().Deserialize<LocationModel>(json);
        }

        return View(location);
    }
}

View

@model IPAddress_Location_MVC.Models.LocationModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr><td>IP</td><td>@Model.IP</td></tr>
        <tr><td>Country Code</td><td>@Model.Country_Code</td></tr>           
    </table>
</body>
</html>
like image 985
MVC Avatar asked Jul 10 '18 03:07

MVC


2 Answers

Like Nilesh pointed out; the response you are getting is valid. ::1 is the short notation for the IPv6 loopback address. This is the equivalent of the IPv4 127.0.0.0 or Localhost. Since you are running it locally and connecting locally it is reporting your Localhost address. If you would run this code on a different machine then your own, and would connect to it from your own machine, you would get a 'real' ip returned.

The second point you mentioned is you get 'The remote server returned an error: (403) Forbidden.' when calling the FreeGeoIP service is caused because that endpoint no longer is in use. (Just surf to that URL: http://freegeoip.net/json/) To be complete, it returns this message:

IMPORTANT - PLEASE UPDATE YOUR API ENDPOINT This API endpoint is deprecated and has now been shut down. To keep using the freegeoip API, please update your integration to use the new ipstack API endpoint, designed as a simple drop-in replacement. You will be required to create an account at https://ipstack.com and obtain an API access key. For more information on how to upgrade please visit our Github Tutorial at: https://github.com/apilayer/freegeoip#readme

Update:

The new API is formatted as:

http://api.ipstack.com/IP_FOR_LOOKUP?access_key=YOUR_ACCESS_KEY&output=json&legacy=1

In order to read the result (Json) you could use a Json deserializer like NewtonSoft. You need to reference the Newtonsoft.Json package in your project and create a POCO that represents the result. Then call Newtonsoft to deserialize the Json as your object. For an example you can check their documentation.

like image 118
RoelA Avatar answered Nov 15 '22 08:11

RoelA


Have you tried getting the IP Address like this?

string ipAddress =  HttpContext.Request.UserHostAddress;

I found this here

like image 37
Hooman Bahreini Avatar answered Nov 15 '22 08:11

Hooman Bahreini