public async Task <IpGeoLocation> VerifyIp(string ip)
        {
            string apiKey         = ConfigurationManager.AppSettings["ApilityApiKey"],
                   ipCheckUrl     = ConfigurationManager.AppSettings["IpCheckUrl"] + ip + "?token=" + apiKey,
                   geoIpLookupUrl = ConfigurationManager.AppSettings["GeoLookupUrl"] + ip + "?token=" + apiKey;

            IpGeoLocation info = new IpGeoLocation();

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(ipCheckUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(ipCheckUrl);

                if (response.IsSuccessStatusCode)
                {
                    //JsonConvert.PopulateObject(response, info);
                }
                else
                {
                }
            }

            return(null);
        }
        public async Task <IpGeoLocation> VerifyIpTest()
        {
            // Retrieve data from file
            string path = HostingEnvironment.MapPath("~\\App_Data\\data.json");

            using (StreamReader r = new StreamReader(path))
            {
                string data = await r.ReadToEndAsync();

                if (!String.IsNullOrEmpty(data))
                {
                    RootObject    rootObject    = JsonConvert.DeserializeObject <RootObject>(data);
                    IpGeoLocation ipGeoLocation = new IpGeoLocation();
                    ipGeoLocation.IpGeoLocationData = rootObject.Ip;
                    ipGeoLocation.BadIp             = false; // TODO: since results it's not a bad ip?

                    if (String.IsNullOrEmpty(rootObject.Ip.Latitude.ToString()))
                    {
                        Console.WriteLine("ERROR");
                    }
                    else
                    {
                        Console.WriteLine();

                        return(ipGeoLocation);
                    }
                }
                r.Close();
            }

            // oops
            return(null);
        }
예제 #3
0
 public static bool CheckIpGeoLocation(this IpGeoLocation ipGeoLocation)
 {
     return(ipGeoLocation != null &&
            !string.IsNullOrEmpty(ipGeoLocation.CountryName) &&
            ipGeoLocation.Latitude.HasValue &&
            ipGeoLocation.Longitude.HasValue);
 }
예제 #4
0
        // GET: IpGeoLocation
        public async Task <ActionResult> Test()
        {
            IpGeoLocation response = await _service.VerifyIpTest();

            IpGeoLocationViewModel model = new IpGeoLocationViewModel
            {
                AutonomousSystemName = response.IpGeoLocationData.As.Name,
                IsBadIp         = false, //TODO: DOUBLE CHECK THIS
                City            = response.IpGeoLocationData.City,
                Continent       = response.IpGeoLocationData.Continent,
                Country         = response.IpGeoLocationData.Country,
                Latitude        = response.IpGeoLocationData.Latitude,
                Longitude       = response.IpGeoLocationData.Longitude,
                PostalCode      = response.IpGeoLocationData.Postal,
                Region          = response.IpGeoLocationData.Region,
                ReverseHostName = response.IpGeoLocationData.Hostname,
                TimeZone        = response.IpGeoLocationData.Time_Zone
            };

            return(View(model));
        }