JavaScript - How to get client MAC address? - Asked By vipul jain on 01-Oct-14 12:56 PM

I have developped a website, containing a poll control using ASP.  When a user votes, a cookie is being written to his machine, so he cannot vote again (unless he deletes the cookie), so the method I am using is not reliable, because any user can delete the cookie and vote again.  Then I tried to retrieve the client mac address, but this is not possible (as I know), unless I use an activeX, but this contains a little issue, the activeX will not run unless the site is in the trusted sites, and this method works only on windows OS since it uses the WMI (since it's a Microsoft component).
Here is my question:
1- Is there any way to get the client mac address, if yes, what is it?
If no:
2- Is there any unique variable related to the client machine (other than the mac address and IP address), can be retrieved?

How to get client MAC address - Kalit Sikka replied to vipul jain on 01-Oct-14 12:53 PM

The C# sample code for getting a client MAC address:

ManagementScope theScope = new ManagementScope("\\\\computerName\\root\\cimv2");

StringBuilder theQueryBuilder = new StringBuilder();
"SELECT MACAddress FROM Win32_NetworkAdapter");
ObjectQuery theQuery = new ObjectQuery(theQueryBuilder.ToString());
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

foreach (ManagementObject theCurrentObject in theCollectionOfResults)

string macAdd = "MAC Address: " + theCurrentObject["MACAddress"].ToString();

other client information - vipul jain replied to Kalit Sikka on 01-Oct-14 12:56 PM

Is there any other unique variable or number on client PC that i can get?

client MAC address - Sagar P replied to vipul jain on 01-Oct-14 12:54 PM

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample Code for Getting Client MAC Address From JavaScript (IE Only)</title>
<script language="javascript">
function showMacAddress(){
	var obj = new ActiveXObject("WbemScripting.SWbemLocator");
	var s = obj.ConnectServer(".");
	var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
	var e = new Enumerator (properties);

	var output;
	output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
	output=output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';

		var p = e.item ();
		if(!p) continue;
		output=output + '<tr bgColor="#FFFFFF">';
		output=output + '<td>' + p.Caption; + '</td>';
		output=output + '<td>' + p.MACAddress + '</td>';
		output=output + '</tr>';

	output=output + '</table>';
	<input type="button" value="Show MAC Address" onclick="showMacAddress()" />

	<div id="box">
Try this MAC Address sample code using winmgmts - ram kumar replied to vipul jain on 01-Oct-14 12:55 PM

var strComputer = ".";
var objWMIService = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
var e = new Enumerator(objWMIService.ExecQuery("Select * from Win32_NetworkAdapter","WQL",48));

for (;!e.atEnd();e.moveNext())
{ objItem = e.item();
 WScript.Echo ("MACAddress: " + objItem.MACAddress)

RE - Web Star replied to vipul jain on 17-Sep-08 04:46 AM

Advanced JavaScript with Internet Explorer: Retrieving Networking Configuration Information - How to retrieve MAC address and other extended network information using JavaScript

(Page 2 of 6 )

Now, let us try to extend the previous sample by showing further information about the network adapters.  The entire code for the sample is as follows:

<!DOCTYPE  HTML  PUBLIC  "-//W3C//DTD HTML 4.0 Transitional//EN">
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="vs_targetSchema" content="http://schemas.">
            <script id="clientEventHandlersJS" language="javascript">

function Button1_onclick() {
      var locator = new ActiveXObject
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
      var e = new Enumerator (properties);
      document.write("<table border=1>");
      for (;!e.atEnd();e.moveNext ())
            var p = e.item ();
            document.write("<td>" + p.Caption + "</td>");
            document.write("<td>" + p.IPFilterSecurityEnabled + "</td>");
            document.write("<td>" + p.IPPortSecurityEnabled + "</td>");
            document.write("<td>" + p.IPXAddress + "</td>");
            document.write("<td>" + p.IPXEnabled + "</td>");
            document.write("<td>" + p.IPXNetworkNumber + "</td>");
            document.write("<td>" + p.MACAddress + "</td>");
            document.write("<td>" + p.WINSPrimaryServer + "</td>");
            document.write("<td>" + p.WINSSecondaryServer + "</td>");

function dispHeading()

            <INPUT id="Button1" type="button" value="Button" 
name="Button1" language="javascript" onclick="return Button1_onclick()">

The explanation for the above code is very similar to that of the previous section. The only difference is that I added few more properties to get in-depth network configuration information.

solution - Perry replied to vipul jain on 17-Sep-08 09:26 AM


To get the IP address you will need to run "ipconfig/all" like below.

$output = `ipconfig /ALL`;  
if($output=~m/((?:[0-9A-F-]{2}-?){6})/i) {
      print "Found Mac address: $1\n";

Other solution is , You can maintain a unique host key concept. This is the way we are identifying the hosts that are in our network. You need to assign encrypted host key in registry to your client machine using Privat and Public key. Only server knows the privat key and all knows public key. Whenever some request goes to server server will identify the host key using private key and validate the same.


Get MAC address JAVASCRIPT code - Perry replied to vipul jain on 17-Sep-08 09:27 AM

use below complete code to get the MAC address.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <title>WMI Scripting HTML</title>

            <script FOR="foo" EVENT="OnCompleted(hResult,
pErrorObject, pAsyncContext)" LANGUAGE="JScript">

            <script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">

         if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)

                   if(objObject.MACAddress != null &&
objObject.MACAddress != "undefined")
                         MACAddr = objObject.MACAddress;

                   if(objObject.IPEnabled && objObject.IPAddress(0) !=
null && objObject.IPAddress(0) != "undefined")
                         IPAddr = objObject.IPAddress(0);

                   if(objObject.DNSHostName != null &&
objObject.DNSHostName != "undefined")
                      sDNSName = objObject.DNSHostName;


                  <FONT color="red"><span ID="info"> </span>. </FONT>
                  <object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" VIEWASTEXT>
                  <object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo">

                  <script LANGUAGE="JScript">

                               var service = locator.ConnectServer();
                               var MACAddr ;
                               var IPAddr ;
                               var DomainAddr;
                               var sDNSName;
                               service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');

<form method="POST" action="/cgi-bin/" id="formfoo" name="formbar">

      <input type="hidden"  name="txtMACAddr">
      <input type="hidden"  name="txtIPAddr">
      <input type="hidden"  name="txtDNSName">



use CGI qw(:standard);

print header,<<ENDHTML;
            Network Interface Card Information Page
            You are at IP Address <STRONG>
            Your MAC address on your network card is <STRONG>
            Your DNS Host name is <STRONG>
            To confirm your IP and MAC address information, go to
the command prompt and
            type in
            ASP reports that your IP Address is <STRONG>
            </STRONG>which is your external WAN IP address that
anyone can see,
            but maybe be shared by hundreds of users if you use Net
Address Translation
            through a common router.

k replied to Perry on 11-Feb-10 10:06 AM
Check this out for a simpler approach.
amit replied to Perry on 02-Aug-10 07:51 AM
hello sir,
i user this script in html page but it's doesn't working with weblogic, as i am using my application with weblogic as a middleware. also if using with out weblogic it shows active X popup message which i should do OK, please suggest me so that no need to do ok and also it will run with weblogic server.
Perry replied to amit on 02-Aug-10 10:01 AM
I think you will need to wrap ActiveX control with some trustworthy dll(like redemption.dll in Outlook). Basically you will need to trust ActiveX control.
shwetha gadde replied to Sagar P on 05-May-12 02:28 AM
 This code is not working for me....
It doesn't show any mac id in my IE wht's the problem pls help me?
Amit Dhorajiya replied to Sagar P on 09-May-13 01:36 AM
This code is absolutely right but when i use this code in php page and run it ,,,in mysql wamp server locally this code is cant give me mac address of client's machine please help me what can i do ................. Please Please Please