Program to detect area or position

Hi,
Is there any programming examples, showing the way to detect which area it is, per given IP (Worldwide IP)?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tapan PattanaikSenior EngineerCommented:
Hi HuaMinChen,

There are may ways like using "GeoIP2 .NET API", http://freegeoip.net/json/ , etc.  For more details, please check the below examples.

Here is the examples:

How to get geographical location by IP address:

https://code.msdn.microsoft.com/How-to-get-geographical-c600a03f

Easy to Track the Geographical Location Based on IP Address.:

https://code.msdn.microsoft.com/Easy-to-Track-the-c2f90dc4

Determine a user's location from their IP Address in ASP.NET Core:

https://www.jerriepelser.com/blog/aspnetcore-geo-location-from-ip-address/


There are also services some paid services.

http://www.ip2location.com/
http://www.maxmind.com/
HuaMin ChenProblem resolverAuthor Commented:
Hi Tapan,
Was there severe problem to MSDN sites in these 2 days?

Do you have example codes to make use of GeoIP2 .NET API, on the Web page?
Tapan PattanaikSenior EngineerCommented:
Hi HuaMinChen,

GeoIP2 .NET API you can find : https://www.jerriepelser.com/blog/aspnetcore-geo-location-from-ip-address/ (with examples)

GeoIP2 .NET API: http://maxmind.github.io/GeoIP2-dotnet/ (with Exmaples)
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Tapan PattanaikSenior EngineerCommented:
In MSDN http://freegeoip.net/json/ is used. Copy the URL and paste in your browser and you will see the response like this.

{"__deprecation_message__":"This API endpoint is deprecated and will stop working on July 1st, 2018. For more information please visit: https://github.com/apilayer/freegeoip#readme","ip":"111.93.154.209","country_code":"IN","country_name":"India","region_code":"KA","region_name":"Karnataka","city":"Bengaluru","zip_code":"","time_zone":"Asia/Kolkata","latitude":12.9833,"longitude":77.5833,"metro_code":0}
Tapan PattanaikSenior EngineerCommented:
Here is the code sample as quoted http://maxmind.github.io/GeoIP2-dotnet/

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = client.Country("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
}

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
Tapan,
Would it be possible to capture current IP of client machine, within Code behind? Do you have such example?
Tapan PattanaikSenior EngineerCommented:
Can you please explain more on your comment.

"Would it be possible to capture current IP of client machine?"
HuaMin ChenProblem resolverAuthor Commented:
I mean the relevant Web page is detecting "current IP" of the user accessing the Web page.
Tapan PattanaikSenior EngineerCommented:
Yes, it is possible. As quoted (https://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net)

protected string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current; 
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
Good day Tapan,
What do adjust, due to this error
Error	13	The type or namespace name 'WebServiceClient' could not be found (are you missing a using directive or an assembly reference?)	

Open in new window


which is encountered in code behind?
HuaMin ChenProblem resolverAuthor Commented:
Hi,
Any help?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.