ASP.NET - Get MAC Address of particular system

Asked By Sharad Chandra on 27-Sep-11 06:36 AM
Hi friends, I am working on get MAC Address, following code returns MAC Address,
but we share application though IIS, and It returns MAC address of system in which application is running,
now i want the MAC address of other system where it is access, pls help me,
I want to track MAC address from where application is accessed
 

public string getMACAddress()

    {

 

    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

    ManagementObjectCollection moc = mc.GetInstances();

    string MACAddress = String.Empty;

    foreach (ManagementObject mo in moc)

    {

      if (MACAddress == String.Empty) // only return MAC Address from first card

      {

      if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();

      }

      mo.Dispose();

    }

 

    MACAddress = MACAddress.Replace(":", "-");

    return MACAddress;

    }

Web Star replied to Sharad Chandra on 27-Sep-11 06:38 AM
you need to get MAC address by ip address like this
Hi please try like this 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.DirectoryServices;
using System.Runtime.InteropServices;
 
namespace GetMacAddressFromIPAddress
{
  public class GetMacAddressFromIPAddress
  {
   [DllImport("iphlpapi.dll", ExactSpelling=true)]
   public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen );
   public string GetMacAddress(string sName)
   {
   string s = string.Empty ;
   System.Net.IPHostEntry Tempaddr = null;
   Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(sName);
   System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
   string[] Ipaddr = new string[3];
   foreach(IPAddress TempA in TempAd)
   {
 Ipaddr[1] = TempA.ToString();
 byte[] ab = new byte[6];
 int len = ab.Length;
 int r = SendARP( (int) TempA.Address, 0, ab, ref len );
 string sMAC = BitConverter.ToString( ab, 0, 6 );
 Ipaddr[2] = sMAC;
 s = sMAC;
   }  
   return s;  
   }
  }
}
Please refer this links
    02-21-2006
http://www.dotnet247.com/247reference/msgs/18/91898.aspx
Suchit shah replied to Sharad Chandra on 27-Sep-11 06:42 AM

ManagementObjectSearcher query1;
ManagementObjectCollection queryCollection1;
ManagementScope RemoteConn;

ObjectQuery oQuery =
new
ObjectQuery("Select MacAddress,IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");

ConnectionOptions options =
new
ConnectionOptions();
options.Username = sDomain + "\\" + sUserName; //could be in domain\user format
options.Password = sPwd;

try
{
RemoteConn = new ManagementScope("\\\\" + RemoteHost + "\\root\\cimv2",options);
query1 = new ManagementObjectSearche(RemoteConn,oQuery);
queryCollection1 = query1.Get();

foreach( ManagementObject mo in queryCollection1 )
{
int nrIP = ((System.Array)(mo["IPAddress"])).Length;

for(int c=0; c<nrIP; c++)
{
Console.WriteLine (RemoteHost + " Mac Address: " + mo["MacAddress"].ToString() );
Console.WriteLine (RemoteHost + " " + c +" IP Address: " + ((System.Array)(mo["IPAddress"])).GetValue(c));
}
}
}
catch (Exception e)
{
Console.WriteLine("Failed to connect to: " + RemoteHost + "\n Error Message: " + e.Message);
}
Reena Jain replied to Sharad Chandra on 27-Sep-11 07:05 AM
Hi,

You can use this code to find MAC id

Public Function GetMACAddress() As String
  Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
  Dim moc As ManagementObjectCollection = mc.GetInstances()
 
  Dim MACAddress As String = [String].Empty
  For Each mo As ManagementObject In moc
  If MACAddress = [String].Empty Then
    ' only return MAC Address from first card
    If CBool(mo("IPEnabled")) = True Then
 
    MACAddress = mo("MacAddress").ToString()
    End If
  End If
  mo.Dispose()
  Next
 
  MACAddress = MACAddress.Replace(":", "")
  Return MACAddress
End Function
smr replied to Sharad Chandra on 27-Sep-11 07:15 AM
hi

To view the MAC address we will be getting, we will need a simple web site that will allow us to display some data. At this point, I have created a new ASP.NET Empty Web Site and need to add in a Web Form with a label. To do this:
  1. Right click the project in your Solution Explorer.
  2. Select Add New Item…
  3. Select a Web Form.
  4. Name it Default.aspx.
  5. Click Add.
  6. Open Default.aspx up to Design mode.
  7. Drag and drop a Label onto the Web Form.
Getting the MAC Address in C#
Next, we need to add some code that will grab the MAC address from the current computer and display it on the web page. To do this:
  1. Open Default.aspx.cs up for editing.
  2. At the top of the class add the following using statement: 
The using statement we need for accessing NetworkInterfaces
using System.Net.NetworkInformation;
 
In the Page_Load event method add in the following code:

The code to display the MAC address.
protected void Page_Load(object sender, EventArgs e)
{
  //get all nics
  NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  //display the physical address of the first nic in the array,
  //which should correspond to our mac address
  Label1.Text = nics[0].GetPhysicalAddress().ToString();
}

refer links also
http://forums.asp.net/t/1054807.aspx/1
http://aspboss.blogspot.com/2010/04/how-to-get-mac-address-using-c.html

Suchit shah replied to Sharad Chandra on 27-Sep-11 07:16 AM
The MACAddress property of the http://msdn.microsoft.com/en-us/library/aa394217.aspx can provide you with an adapter's MAC address.

(System.Management Namespace)

f you're not familiar with the WMI API (Windows Management Instrumentation), there's a http://msdn.microsoft.com/en-us/library/bb404655.aspx for .NET apps.

WMI is available across all version of windows with the .Net runtime.

Here's a code example:

 

System.Management.ManagementClass mc = default(System.Management.ManagementClass);
ManagementObject mo = default(ManagementObject);
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (var mo in moc) {
if (mo.Item("IPEnabled") == true) {
Adapter.Items.Add("MAC " + mo.Item("MacAddress").ToString());
}
}

Regards

Sharad Chandra replied to Web Star on 28-Sep-11 02:20 AM
thanks it help me to solve my problem