2
votes

I want to get the IP Address and the MAC Address of the user who login to the system (web application). I am using these to line of codes

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

I am using this method to get the MAC address, but the problem is I am getting the MAC Address of all the Adapters of the computer.

HOW do I know which one is the right one?, how to get the right MAC Address of the device that is connected to my website??

and also how to get the IP of the adapter too.

when i try it on my pc it give 8 adapters with 8 MAC Addresses

I am trying on my pc, I have a wired connection connected to IntraNet, and a wireless connected to internet.

5
5 tags and none of them telling what language you're using?Wooble
OP might have thought C# is the default language msdn.microsoft.com/en-us/library/…Roman R.
I am using C# I will EDIT the title and the content and the tags right now, thank you for the NOTE :)msytNadeem
@RomanR. dude, I know, but how i could know which one is connected to the website, if the user login to my website, i want to get the mac address to store it in the database, which one should i take?msytNadeem
webforms, winforms, wpf, silverlight... ?balexandre

5 Answers

3
votes

You can never get the MAC address of a user connected to your website, when a socket connection occurs between the destination (your website's server) and a source (client's computer) you can only get the source IP address, for the MAC address it's never sent over the socket connection (Client <--> Server), in order to get the IP address of a user:

using System.Net;

Private string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;

return addr[addr.Length-1].ToString();

}

source: Here

1
votes
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);

private static string GetClientMAC(string strClientIP) {
    string mac_dest = "";
    try {
        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");

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

        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));
                }
            }
        }
    } catch (Exception err) {
        throw new Exception("Lỗi " + err.Message);
    }
    return mac_dest;
}

This code will help you :)

0
votes

You can get the MAC address using below code snippet. To implement this function you need to add System.Management namespace in your application. If it is not available you need to add its reference from "Add references" from your project.

using System.Management;
using System.IO;

public string GetMACAddress()
{
    string mac_src = "";
    string macAddress = "";

    foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
        {
            mac_src += nic.GetPhysicalAddress().ToString();
            break;
        }
    }

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

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

When IIS Deploy this application with code ..run successfully

   public string GetIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }
    [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);

    private static string GetClientMAC(string strClientIP)
    {
        string mac_dest = "";
        try
        {
            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");

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

            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));
                    }
                }
            }
        }
        catch (Exception err)
        {
            throw new Exception("Lỗi " + err.Message);
        }
        return mac_dest;
    }

 protected void Button1_Click1(object sender, EventArgs e)
    {  
        Label1.Text = GetClientMAC(GetIPAddress());
    }
-2
votes

First of all, would be nice to know why do you want the MAC Address of your clients (users) as Mac Addresses are NOT UNIQUE.

You can only use the .NET Framework to get the SERVER Connection, and never the User it self, for that, for getting the client MAC Address you can only rely on javascript and ActiveX control, witch can only be used on Internet Explorer as Firefox already deprecated it's support to ActiveX controls several years ago.

A good read about this can be found in this article.