In this article I will explain with an example, how to find visitors geographic location like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using IP Address in ASP.Net using C# and VB.Net.
 
 
Generate API Key
The IPInfoDB IP to Location API is a FREE API to get the Geographic location details such as Country, City, Region, Zip Code, Latitude, Longitude and Time zone from IP Address.
In order to use the API, you will need to register and get an API Key. Once the API Key has been generated the API can be accessed using the following URL.
The API returns Geographic location details in JSON format.
 
 
API URL
Generated JSON string without API Key
The API URL without the Key will look like this.
Find Visitors Geographic Location using IP Address in ASP.Net
 
 
Generated JSON string with API Key
If you use API key it will be displayed as shown below.
{
      "statusCode" : "OK",
      "statusMessage" : "",
      "ipAddress" : "74.125.45.100",
      "countryCode" : "US",
      "countryName" : "UNITED STATES",
      "regionName" : "CALIFORNIA",
      "cityName" : "MOUNTAIN VIEW",
      "zipCode" : "94043",
      "latitude" : "37.3861",
      "longitude" : "-122.084",
      "timeZone" : "-07:00"
}
 
 
HTML Markup
The following HTML Markup consists of:
GridView - For displaying geographic location details.
GridView consists of eight BoundField columns.
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
        <asp:BoundField DataField="CountryName" HeaderText="Country" />
        <asp:BoundField DataField="CityName" HeaderText="City" />
        <asp:BoundField DataField="RegionName" HeaderText="Region" />
        <asp:BoundField DataField="CountryCode" HeaderText="Country Code" />
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Longitude" />
        <asp:BoundField DataField="Timezone" HeaderText="Timezone" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Web.Script.Serialization;
 
VB.Net
Imports System.Net
Imports System.Web.Script.Serialization
 
 
Public Property Class
The following pulic class Location consists of the following property to hold the geographic location details returned from the API.
C#
public class Location
{
    public string IPAddress { get; set; }
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    public string CityName { get; set; }
    public string RegionName { get; set; }
    public string ZipCode { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string TimeZone { get; set; }
}
 
VB.Net
Public Class Location
    Public Property IPAddress As String
    Public Property CountryName As String
    Public Property CountryCode As String
    Public Property CityName As String
    Public Property RegionName As String
    Public Property ZipCode As String
    Public Property Latitude As String
    Public Property Longitude As String
    Public Property TimeZone As String
End Class
 
 
Fetching and displaying the Location Details for the IP Address
Inside the Page Load event handler, first the IP Address is determined.
Note: For more details on fetching IP Address, please refer my article How to get IP Address of Visitors Machine in ASP.Net.
 
The IP Address is then sent as parameter along with the API Key to the IPInfoDB IP to Location API which is being called using the DownloadString method of the WebClient class.
Note: For previous .Net Framework versions ,please refer Using TLS1.2 in .Net 2.0, .Net 3.0, .Net 3.5 and .Net 4.0.
 
Then, an object of Location public class is created and received geographic location details in JSON format is deserialized into the Location class object and added as location.
Finally, the Location class object is assigned to the DataSource property of the GridView and GridView is populated with geographic location details.
C#
protected void Page_Load(object sender, EventArgs e)
{
    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ipAddress))
    {
        ipAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
 
    string apiKey = "<Your API Key>";
    string url = string.Format("https://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", apiKey, ipAddress);
 
    //Setting TLS 1.2 protocol.
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
 
    using (WebClient client = new WebClient())
    {
        string json = client.DownloadString(url);
        Location location = new JavaScriptSerializer().Deserialize<Location>(json);
        List<Location> locations = new List<Location>();
        locations.Add(location);
        gvLocation.DataSource = locations;
        gvLocation.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If String.IsNullOrEmpty(ipAddress) Then
        ipAddress = Request.ServerVariables("REMOTE_ADDR")
    End If
 
    Dim apiKey As String = "<Your API Key>"
    Dim url As String = String.Format("https://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", apiKey, ipAddress)
 
    'Setting TLS 1.2 protocol.
    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
 
    Using client As New WebClient()
        Dim json As String = client.DownloadString(url)
        Dim location As Location = New JavaScriptSerializer().Deserialize(Of Location)(json)
        Dim locations As New List(Of Location)()
        locations.Add(location)
        gvLocation.DataSource = locations
        gvLocation.DataBind()
    End Using
End Sub
 
 
Screenshot
Find Visitors Geographic Location using IP Address in ASP.Net
 
 
Demo
 
 
Downloads