Sunday 8 April 2018

how to get User Location by IP address in asp.net using C#

how to get User Location by IP address in asp.net using C#

Description:


In this example we explain that how to get User Location by IP address in asp.net using C#. or how to get country based on IP Address in asp.net using C#. or how to get user machine address information like (country, IP address, State, Zip code etc..) where the user actually accesses our website using C#.

So here we demonstrate that how to get end user Location information through web service in C#. below is the code that are useful to get all address related information of the client PC.
Code:

public static string CityStateCountByIp(string IP)
    {
        var url = "http://freegeoip.net/json/" + IP;
        var request = System.Net.WebRequest.Create(url);

        using (WebResponse wrs = request.GetResponse())
        using (Stream stream = wrs.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            string json = reader.ReadToEnd();
            var obj = JObject.Parse(json);
            var City = (string)obj["city"];
            // - For Country = (string)obj["region_name"];                   
            //- For  CountryCode = (string)obj["country_code"];

            return (City);
        }

        return "";

    }



0 comments:

Post a Comment