Friday, 25 March 2016

Get User Detail Like City,County,Region,Zip Code Using IP Address In Asp.Net

3/25/2016 - By Pranav Singh 2

This will you how you can get the user detail like city name, country name, country code, city name, region name, zip code and time zone by users IP address in asp.net using c#.

Some of my previous articles are as follows: Facebook Style Lightbox jQuery Plugin To Display Images Like Facebook In Asp.Net, Webcam Integration Using jQuery Webcam Plugin In Asp.Net, Asp.Net Dynamic jQuery Photo Gallery Plugin With Zoom Effect Using Css, Magnifying Glass Effect Of Image Using jQuery in MVC, Social Share Buttons of Facebook, Twitter, Google Plus and Pinterest Using jQuery in Asp.Net MVC.

So for this article first we will register to http://ipinfodb.com website to get the location API key.  This is one of the most important step for completing this article. Without API we will not be able to procide. This url we are going to use get the user city name by his IP address.

Now we will create a new asp.net application and dd the below page code into the .aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get User Detail Like City,County,Region,Zip Code Using IP Address In Asp.Net
    </title>
</head>
<body style="text-align: center">
    <form id="form1" runat="server">
    <table width="100%" border="1">
        <tr>
            <td align="left" style="font-weight: bold;">
                IP Address:
            </td>
            <td align="left">
                <asp:Label ID="lblIpaddress" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Country Name:
            </td>
            <td align="left">
                <asp:Label ID="lblCountryname" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Country Code:
            </td>
            <td align="left">
                <asp:Label ID="lblcountrycode" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                City Name:
            </td>
            <td align="left">
                <asp:Label ID="lblCitynae" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Region Name:
            </td>
            <td align="left">
                <asp:Label ID="lblRegionname" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Zip Code:
            </td>
            <td align="left">
                <asp:Label ID="lblZipcode" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Latitude:
            </td>
            <td align="left">
                <asp:Label ID="lblLatitude" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Longitude:
            </td>
            <td align="left">
                <asp:Label ID="lblLongitude" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="left" style="font-weight: bold;">
                Time Zone:
            </td>
            <td align="left">
                <asp:Label ID="lblTimezone" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Now check the .cs page code to get the detail.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Location objlocation = new Location();
            objlocation = Location();

            lblIpaddress.Text = objlocation.IPAddress;
            lblCountryname.Text = objlocation.CountryName;
            lblcountrycode.Text = objlocation.CountryCode;
            lblCitynae.Text = objlocation.CityName;
            lblRegionname.Text = objlocation.RegionName;
            lblZipcode.Text = objlocation.ZipCode;
            lblLatitude.Text = objlocation.Latitude;
            lblLongitude.Text = objlocation.Longitude;
            lblTimezone.Text = objlocation.TimeZone;
        }

        /// <summary>
        /// get user detail bu ip
        /// </summary>
        /// <returns></returns>
        private Location Location()
        {
            string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = Request.ServerVariables["REMOTE_ADDR"];
            }

            string APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
            WebClient client = new WebClient();
            string json = client.DownloadString(url);
            Location location = new JavaScriptSerializer().Deserialize<Location>(json);
            return location;
        }
    }

    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; }
    }
}

In above code I have get the user IP address and then pass it to the API to get the detail into json format. After getting the detail I have stored the detail into the list
Now we have done run the application to check the output.



Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    Digital Marketing Training

    ReplyDelete
  2. Very useful for and effective blog for us..I always read your all blogs..
    For any Graphtec Dealers in India
    For any Graphtec High Speed Data Logger

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top