How To: Get Client Machine IP Address | MAC Address using Javascript, JSON, Jquery, ASP.NET Server-Side

What is an IP Address? How to track user information using IP Address?

An IP address is an address for a computer on the Internet.

So does this IP Address be useful for tracking user information?

Just going into a brief about IP Address – Every device on an TCP/IP network must have a unique IP address. IP addresses are assigned, either automatically by DHCP, or by manual configuration.

However, IP Addresses can be easily tampered with using a router or a proxy server. So it becomes very difficult to track IP Address in such cases.

Here, in the below example, I will simply show you a way to get the client machine IP Address, MAC Address & HostName using Javascript, JSON, Jquery, ASP.NET Server-Side.

Note : This wont give you the exact IP Address in cases where requests are sent through an ISP server or a proxy.

Get Client IP Address using Javascript, JSON, Jquery, ASP.NET

Get Client IP Address using Javascript, JSON, Jquery, ASP.NET

1. Get Client IP Address, MAC Address & DNS HostName using Javascript:

<script type="text/javascript">
var macAddress = "";
var ipAddress = "";
var computerName = "";
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
e = new Enumerator(wmi.ExecQuery
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
for(; !e.atEnd(); e.moveNext()) {
var s = e.item();
macAddress = s.MACAddress;
ipAddress = s.IPAddress(0);
computerName = s.DNSHostName;
}
alert(macAddress);
alert(ipAddress);
alert(computerName);
</script>

Here, we have used Win32_NetworkAdapterConfiguration, which is a WMI class, to read network related details like the MAC Address, IP Address and the computer name.
More on Win32_NetworkAdapterConfiguration can be found here – http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx

2. Get Client IP Address, MAC Address & DNS HostName using Javascript & JSON:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
    window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
    }
</script>
</head>
<body>
    <form>
        <span id = "ipaddress"></span>
    </form>
</body>
</html>

Here, we are making a JSON call to a Free Web Service http://jsonip.appspot.com/ – This type of services will allow you to look up the physical location of a user based on their IP address, also called IP geolocation.
Alongwith that, passing the callback function which returns the client IP address making the request which is displayed in the span tag.

3. Get Client IP Address, MAC Address & DNS HostName using Jquery & JSON:

<script type = "text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">
    $(function () {
        $.getJSON("http://jsonip.appspot.com?callback=?", DisplayIP);
    });
    function DisplayIP(response) {
        $("#ipaddress").html("Your IP Address is " + response.ip);
    };
</script>
<span id = "ipaddress"></span>

This is similar to the above just that we are using Jquey here.

4. Get Client IP Address, MAC Address & DNS HostName using ASP.NET Server-side:

private string IpAddress()
{

string strIpAddress;

strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (strIpAddress == null)

strIpAddress = Request.ServerVariables["REMOTE_ADDR"];

return strIpAddress;

}

When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine.
Hence first we need to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address.
If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.

Note: Also while running the code on your machine will return IP Address as 127.0.0.1. But once deployed on server, it will return the correct IP Address.

If you want to display the correct IP address of your machine using ASP.NET server side code, then use the below:

// Track Visitors
string ipAddress = IpAddress();

string hostName = Dns.GetHostByAddress(ipAddress).HostName;

string strHostName = System.Net.Dns.GetHostName();
string clientIPAddressN = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
string clientipN = clientIPAddressN.ToString();

Here, the string clientipN will return you your machine’s IP Address.
As you can see, we have used the System.Net.Dns class which provides the different methods for getting IP Address, HostName,etc.

Hope this post will be useful. Do share your feedback.

Related Posts Plugin for WordPress, Blogger...

Enter your email address:

Delivered by FeedBurner

Share This Post

Related Articles

© 2014 The Opening Batsman. All rights reserved. Site Admin · Entries RSS · Comments RSS
Visit blogadda.com to discover Indian blogs Powered by WordPress · Designed by Theme Junkie
seo seo consulting escalateseo seo basics seo tools seo tips seo tutorial seo services google seo seo software seo forum