Skip to content

How to access the MAC address of the system?

Ashutosh Tripathi

Ashutosh Tripathi

Asked Aug 25, 2015, 8:56 AM EDT9 answers
Share
LinkedInX
How to access the MAC address of the system?
 
I have a code which is working fine but only on localhost. After publishing my website on server, it cannot access the MAC Address and either throw an exception object reference not set to an instance of an object  or after using try catch it move to catch block.
 
I am providing my code, so that anyone can suggest me something fruitful:
 
C#
public string GetMACAddress()
        {
            try
            {
                ManagementClass objMOS = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMOS.GetInstances();
                string MACAddress = String.Empty;
                foreach (ManagementObject objMO in objMOC)
                {
                    if (MACAddress == String.Empty) // only return MAC Address from first card
                    {
                        MACAddress = objMO["MacAddress"].ToString();
                    }
                    objMO.Dispose();
                }
                MACAddress = MACAddress.Replace(":", "");
                return MACAddress;
            }
            catch (Exception ei)
            {
                return "No Mac";
            }
        }

9 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Ken H
    0
    Hi,
       Since the code is running on the server, so you can not get the client MAC address.. 
    0
  • Ashutosh Tripathi
    0
    Hi Ken H,
    Thank you for your reply, but i need something which can act like this.
    The motive behind this thought is:
     
    I am developing an ecommerce site where i need to provide a service of add to cart    
    without login into website, for which i use mac address as a unique identity for every customer.
    So please suggest me some alternative option for this same work.
    0
  • Ken H
    0
    Please  try:
     
    TypeScript
    namespace GetClientMacAddressDemo
    {
        public partial class _Default : Page
        {
            [DllImport("Iphlpapi.dll")]
            private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    C#
            private static extern Int32 inet_addr(string ip);
            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    string userIP = Request.UserHostAddress;
                    string sClientIP = Request.UserHostAddress.ToString().Trim();
                    Int32 dest = inet_addr(sClientIP);
                    Int64 mac = new Int64();
                    Int32 len = 6;
    
                    int res = SendARP(dest, 0, ref mac, ref len);
                    string gmac = mac.ToString("X");
    
                    // if odd number then fill '0'.
                    if ((gmac.Length & 1) == 1) {
    
                        gmac=gmac.Insert(0, "0");
                    }
    
                    gmac = gmac.Reverse();
    
                    Response.Write("Hello,Your MAC Address Is:" + gmac);
    
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
        }
    
        // Extension method for string reverse.
        public static class ExString
        {
    
            public static string Reverse(this string targetString)
            {
                if (String.IsNullOrEmpty(targetString.Trim())
                    && (targetString.Length/2)<=1) return String.Empty;
    
                string szTEMP = String.Empty;
                int i = 0;
    
                for (i=targetString.Length;i>=4;i-=2)
                {
                    szTEMP += targetString.Substring(i - 2, 2)+"-";
                }
                szTEMP += targetString.Substring(i - 2, 2);
    
                i &= 0;
    
                return szTEMP;
            }
        }
    }
     
    0
  • Ashutosh Tripathi
    0
    Thank you sir for your reply, can you please elaborate me your codes so that i can easily modify it according my requirement in mvc.
    0
  • Ken H
    0
    It only calls WINDOWS API,Such as dynamic link library 'Iphlpapi.dll' and 'Ws2_32.dll'.
    Explanation:
    Iphlpapi:IP Helper API,IP secondary interface.
    Ws2_32: Windows Socket 2.0,Internet and network applications to support.
    You can used in page load event.
     
    I hope to help you. 
    0
  • Ashutosh Tripathi
    0
    Hey,
    I am trying to use your code but on [DllImport]  it is showing error, that the file could not be found. And also show error on Request and Response, so please guide me.
     
    If you provide this same code in form of class file, that will be more comfortable for me.
    0
  • Ken H
    0
    C#
    // class file.
    namespace Demo
    {
        public static class GetClientMacAddress
        {
            // 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")]
    C#
            private static extern Int32 inet_addr(string ip);
            /// <summary>
            /// Get client mac address.
            /// </summary>
            /// <param name="userHostAddress">User host address.</param>
            /// <param name="szClientIP">Client IP.</param>
            /// <returns></returns>
            public static string GetCMA(string userHostAddress, string szClientIP)
            {
                try
                {
                    Int64 mac = new Int64();
                    Int32 len = 6;
                    Int32 dest = inet_addr(szClientIP);
    
                    int res = SendARP(dest, 0, ref mac, ref len);
    
                    string gmac = mac.ToString("X");
    
                    if ((gmac.Length & 1) == 1)
                    {
                        gmac = gmac.Insert(0, "0");
                    }
    
                    return (String)(gmac.Reverse());
    
                }
                catch (Exception)
                {
                    return String.Empty;
                   // throw;
                }
    
            }
    
        }
        // Extension method for string reverse.
        public static class ExString
        {
            public static string Reverse(this string targetString)
            {
                if (String.IsNullOrEmpty(targetString.Trim())
                && (targetString.Length / 2) <= 1) return String.Empty;
                string szTEMP = String.Empty;
                int i = 0;
                for (i = targetString.Length; i >= 4; i -= 2)
                {
                    szTEMP += targetString.Substring(i - 2, 2) + "-";
                }
                szTEMP += targetString.Substring(i - 2, 2);
    
                i &= 0;
    
                return szTEMP;
            }
        }
    }
    0
  • Ashutosh Tripathi
    0
    hello sir,
     Please tell some things about your code.
    1) What value i should pass to  function
     
    C#
    public static string GetCMA(string userHostAddress, string szClientIP)
    {
    
    }
     
    I am using  
    string userIP = Request.UserHostAddress;
    string sClientIP = Request.UserHostAddress.ToString().Trim();
     
    but on Request it show error that The name"Request" not exist in current context.
     
     
    So please guide me. 
    0
  • Ken H
    0
    Please  try: 
     
            System.Web.HttpContext.Current.Request.UserHostAddress.ToString().Trim();
    0