Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi to all,
How to get client mac address. here i using this below script but its works only in ie not in all browser. and this code i hosted in server but not retrieve any value even in ie also.


SCRIPTS:-
C#
function callme() {
           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);
               document.getElementById('ctl00_cplhControlPanel_macAddress').value = macAddress;
           }
       }



here this function call in onclientevent. kindly help me in all browser
Posted

You can't - MAC addresses do not travel beyond the router, so you have to run code on the client. And the only ways to do that I know of are IE specific.

And to be honest, it probably won't help you: MAC addresses aren't unique (they only have to be unique within a LAN segment) and they are very easy to spoof. So if you are planning on using MAC for identification / authorization, I would recommend you forget it and install a cookie instead.
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 20-Jun-15 3:25am    
cookies means?? explain me please
OriginalGriff 20-Jun-15 3:44am    
https://msdn.microsoft.com/en-us/library/ms178194(v=vs.140).aspx
i got it here i used

(using System.Runtime.InteropServices;)
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
protected void Page_Load(object sender, EventArgs e)
{
try
{
string userip = Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString().Trim();
Int32 ldest = inet_addr(strClientIP);
Int32 lhost = inet_addr("");
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
string mac_src = macinfo.ToString("X");
if (mac_src == "0")
{
if (userip == "127.0.0.1")
Response.Write("visited Localhost!");
else
Response.Write("the IP from" + userip + "" + "
");
return;
}

while (mac_src.Length < 12)
{
mac_src = mac_src.Insert(0, "0");
}

string mac_dest = "";

for (int i = 0; i < 11; i++)
{
if (0 == (i % 2))
{
if (i == 10)
{
mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
}
else
{
mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
}
}
}

Response.Write("welcome" + userip + "
" + ",the mac address is" + mac_dest + "."

+ "
");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}


 
Share this answer
 
Comments
F-ES Sitecore 20-Jun-15 5:31am    
That code will only get the MAC address of the client if you're running an intranet on the same LAN as the client, over the internet it'll give you the MAC of a router.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900