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";
}
}
Ken HRank #2,999 · 474 pointsPosted Aug 25, 2015, 7:26 PM EDT
Ashutosh Tripathi35 pointsPosted Aug 26, 2015, 11:56 AM EDT
Ken HRank #2,999 · 474 pointsPosted Aug 26, 2015, 11:56 PM EDT
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);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; } } }Ashutosh Tripathi35 pointsPosted Aug 27, 2015, 4:48 AM EDT
Ken HRank #2,999 · 474 pointsPosted Aug 27, 2015, 5:45 PM EDT
Ashutosh Tripathi35 pointsPosted Aug 31, 2015, 10:43 AM EDT
Ken HRank #2,999 · 474 pointsPosted Aug 31, 2015, 10:05 PM EDT
// 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);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; } } }Ashutosh Tripathi35 pointsPosted Sep 17, 2015, 6:55 AM EDT
public static string GetCMA(string userHostAddress, string szClientIP) { }Ken HRank #2,999 · 474 pointsPosted Sep 21, 2015, 5:40 PM EDT