Amazon Ad

Wednesday, 16 April 2014

How to get client's MAC Address from browser


Hi Guys,

I was having problem of finding out MAC address from browser. I finally achieved the same by using help from a blog and jQuery. The current code is tested on local LAN and it gives the MAC address based on the local LAN ip address.

Here is how i achieved it





Step 1 :

Create a utility class
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;

//Thanks to http://stephenhaunts.com/2014/01/06/getting-the-mac-address-for-a-machine-on-the-network/
namespace MAC
{
    public sealed class NativeMethods
    {
        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        private static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

        private NativeMethods() { }

        public static string GetMacAddress(string ipAddress)
        {
            if (string.IsNullOrEmpty(ipAddress))
            {
                throw new ArgumentNullException("ipAddress");
            }

            IPAddress IP = IPAddress.Parse(ipAddress);
            byte[] macAddr = new byte[6];
            uint macAddrLen = (uint)macAddr.Length;

            if (SendARP((int)IP.Address, 0, macAddr, ref macAddrLen) != 0)
            {
                throw new Exception("ARP command failed");
            }

            string[] str = new string[(int)macAddrLen];

            for (int i = 0; i < macAddrLen; i++)
            {
                str[i] = macAddr[i].ToString("x2");
            }

            return string.Join(":", str).ToUpper();
        }
    }
}

Step 2:

Create a default.aspx page and do the following code in c#.
[WebMethod]
        public static string GetMAC(string ip)
        {
            string mac=MAC.NativeMethods.GetMacAddress(ip);
            return mac;
        }

Step 3:

Calling webmethod from browser (client side) to get mac address

<script language="javascript">
        var ip = '<%= Request.UserHostAddress%>';
        $.ajax({
            data: "{'ip':'"+ip+"'}",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            url: "Default.aspx/GetMAC",
            success: function (data) {
                $("#macdiv").html("Your IP is " + ip + " Your MAC Address is <b>" + data.d + "</b>");
            },
            error: function (a, b, c) { alert(a); }
        });
    </script>

That's all folks, Please do give your feedback on the same.

Thanks
Ritesh Tandon

8 comments:

Unknown said...

ARP Command Failed Everytime

Unknown said...

ARP failed Every Time

Prashant said...

its not working its give server side mac address not client side mac

san said...

not working...

Unknown said...

so it works on the same subnet otherwise does not.

Unknown said...

Enter your comment...

Anonymous said...

Yes guys it works on same subnet as I have already mentioned above "The current code is tested on local LAN".

JAAT said...

not working. It only shows the MAC address of hosting Server not the Client Machine.

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...