Get MAC Address from application client machine

Asked

Viewed 12,403 times

14

I would like to know the best way to get the MAC Address from the client of my Asp.Net Web Forms application. As it is a low level information, I believe it is not as simple as it seems.

3 answers

10


It is not possible to obtain this information, as neither Asp.net has access to this information from the client’s machine nor Javascript, as it is not trafficked as part of the Http protocol.

The only way to obtain this information would be through an Activex or Java applet component that would need to be installed on the client’s machine and the appropriate security settings applied.

  • So in my case the solution will be to create an Applet since using Activex would work only in Internet Explorer.

  • 1

    If Voce wants to get the client’s Mac Address for authentication reasons, Voce can for example use a cookie with some unique identifier (GUID type) for this. It would be lighter than creating an applet.

  • My intention to get the Mac Address is to create a Log in which will be stored data from the client’s machine at login time.

2

If your clients are on the same LAN as you (for example, on an intranet), it may be that your MAC addresses are present in the router’s ARP table.

In a Windows environment, you can check the ARP table via command ARP -a:

inserir a descrição da imagem aqui

The class System.Diagnostics.Process allows you to perform a process on your server; optionally, you can redirect the generated content to a text stream (via StartInfo.RedirectStandardOutput = true).

The function below performs the steps described to get the MAC Address from an IP address:

public string GetMacAddress(string ipAddress)
    {
        string macAddress = string.Empty;
        System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
        pProcess.StartInfo.FileName = "arp";
        pProcess.StartInfo.Arguments = "-a " + ipAddress;
        pProcess.StartInfo.UseShellExecute = false;
        pProcess.StartInfo.RedirectStandardOutput = true;
          pProcess.StartInfo.CreateNoWindow = true;
        pProcess.Start();
        string strOutput = pProcess.StandardOutput.ReadToEnd();
        string[] substrings = strOutput.Split('-');
        if (substrings.Length >= 8)
        {
           macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2)) + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6] + "-" + substrings[7] + "-" +
                  substrings[8].Substring(0, 2);
            return macAddress;
        }

        else
        {
            return "not found";
        }
    }
  • But my application is extranet, I need Mac Address for Log purposes.

  • As it was not mentioned in the original post I imagined that this would be a possible scenario, @William. Anyway I’ll leave the answer here - maybe it will be useful to someone in the future.

2

Friends, by my mistake I didn’t realize that it was a WEB application.

Follow the javascript solution to list MAC addresses, however it uses an Activex and will only work in IE. I will search for you if there is any multi-browser solution.

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Getting MAC Address From Javascript(IE Only)</title>

    <script language="javascript">
    function showMacAddress(){

        var obj = new ActiveXObject("WbemScripting.SWbemLocator");
        var s = obj.ConnectServer(".");
        var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
        var e = new Enumerator (properties);


        var output;
        output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
        output=output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
        while(!e.atEnd())

        {
            e.moveNext();
            var p = e.item ();
            if(!p) continue;
            output=output + '<tr bgColor="#FFFFFF">';
            output=output + '<td>' + p.Caption; + '</td>';
            output=output + '<td>' + p.MACAddress + '</td>';
            output=output + '</tr>';
        }

        output=output + '</table>';
        document.getElementById("box").innerHTML=output;
    }
    </script>

    </head>
    <body>
        <input type="button" value="Show MAC Address" onclick="showMacAddress()" />

        <div id="box">
        </div>
    </body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.