0

I am trying to use the below code to get the users IP to understand what country they are within but when i publish my MVC Core to an app service in Azure, the ip address is coming back as an IP from Netherlands and assigned to Microsoft Corporation, is this because it is hosted in the cloud and it is pulling the cloud IP? If so how do i get around this?

string info = new WebClient().DownloadString("http://ipinfo.io");
            return JsonConvert.DeserializeObject<IpInfo>(info);
bobby
  • 183
  • 9
  • Umm, you are making the request from the server right? So it would be the server's IP address then? – juunas Aug 04 '20 at 13:22
  • Thats not how you do it. As @juunas said that will just give you the IP of the Server. If you need to get the IP of the visitor on your site you first need to catch that and first after that you will be able to use ipinfo.io API to get more Information. You need to do some more research how to do this since you are way of with your question today. – Daniel Björk Aug 04 '20 at 13:25
  • Have a look at this: https://stackoverflow.com/questions/28664686/how-do-i-get-client-ip-address-in-asp-net-core – Daniel Björk Aug 04 '20 at 13:27

2 Answers2

1

Try below code, it is available on site: https://dotnetfiddle.net/4LRFYp

using System;
using System.Net;
using Newtonsoft.Json;

public class GetGeoLocation
{
    public IPData GetIPGeoLocation(string IP)
    {
        WebClient client = new WebClient();
        // Make an api call and get response.
        try
        {
            string response = client.DownloadString("http://ip-api.com/json/" + IP);
            //Deserialize response JSON
            IPData ipdata = JsonConvert.DeserializeObject<IPData>(response);
            if (ipdata.status == "fail")
            {
                throw new Exception("Invalid IP");
            }

            return ipdata;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public static void Main()
    {
        IPData ipdata = new GetGeoLocation().GetIPGeoLocation("208.80.152.201");
        Console.WriteLine(ipdata.status + " - Your IP belongs to '" + ipdata.region + " - " + ipdata.country + "'");
    }
}

public class IPData
{
    public string status { get; set; }
    public string country { get; set; }
    public string countryCode { get; set; }
    public string region { get; set; }
    public string regionName { get; set; }
    public string city { get; set; }
    public string zip { get; set; }
    public string lat { get; set; }
    public string lon { get; set; }
    public string timezone { get; set; }
    public string isp { get; set; }
    public string org { get; set; }
    public string @as { get; set; }
    public string query { get; set; }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
seema
  • 29
  • 1
  • 7
0

Here is the code you can use:

    var origin = Request.Headers.ContainsKey("CLIENT-IP") ? Request.Headers["CLIENT-IP"][0] : "";

where Request is HTTP request object