How To Get IP & Locations details of client machine in asp.net

Using JQuery

Add this code in head section

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

    <script type="text/javascript">
        $.get("http://ipinfo.io/66.87.90.80", function (response) {
            document.getElementById("<%=Label1.ClientID %>").innerHTML = "";
            document.getElementById("<%=Label1.ClientID %>").innerHTML = "IP is : " + response.ip + "<br> Host Name is : " + response.hostname
            + "<br> Host Name is : " + response.hostname
            + "<br> City Name is : " + response.city
            + "<br> Region Name is : " + response.region
            + "<br> Country Name is : " + response.country
            + "<br> Loc Name is : " + response.loc
            + "<br> Postal Name is : " + response.postal;
        }, "jsonp");

    </script>

Add this line of code inside form

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Text="Get IP Address" />

-------------------------------------------------

Using C# Code

protected void Page_Load(object sender, EventArgs e)
        {
            string ipaddress;
            ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (ipaddress == "" || ipaddress == null)
                ipaddress = Request.ServerVariables["REMOTE_ADDR"];
         
            GetPublicIpAddress(ipaddress);
           

        }

//Response in string format

  private void GetPublicIpAddress(string ipaddress)
        {
            var request = (HttpWebRequest)WebRequest.Create("http://ipinfo.io/" + ipaddress);

            request.UserAgent = "curl"; // this simulate curl linux command

            string publicIPAddress;

            request.Method = "GET";
            using (WebResponse response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    publicIPAddress = reader.ReadToEnd();
                }
            }

            Response.Write(publicIPAddress.Replace("\n", ""));
        }

//Response in json serialize 

public void GetIPInfo(string ipaddress)
        {
            string URL = "http://ipinfo.io/" + ipaddress;
            using (WebClient client = new WebClient())
            {
                string json = client.DownloadString(URL);
                IPInfo ipinfo = new JavaScriptSerializer().Deserialize<IPInfo>(json);
                Label1.Text = "IP is : " + ipinfo.ip + "<br> Host Name is : " + ipinfo.hostname
                + "<br> Host Name is : " + ipinfo.hostname
                + "<br> City Name is : " + ipinfo.city
                + "<br> Region Name is : " + ipinfo.region
                + "<br> Country Name is : " + ipinfo.country
                + "<br> Loc Name is : " + ipinfo.loc
                + "<br> Postal Name is : " + ipinfo.postal;
            }

        }

public class IPInfo
    {
        public string ip { get; set; }
        public string hostname { get; set; }
        public string city { get; set; }
        public string region { get; set; }
        public string country { get; set; }
        public string loc { get; set; }
        public string org { get; set; }
        public string postal { get; set; }
    }

Comments

Popular posts from this blog

Executing PowerShell scripts from C#

HOW TO Use the NumericUpDown Control

Exposing Agile Software Development Myths