Thursday, December 25, 2014

ASP.net - MAC address of Client Machine in ASP.net

In this article i will show you how to get MAC address from Client machine with the help of WMI Library and Jquery in ASP.net project.

In my previous article i explained about how to get MAC address using C#.net,here is following Link
http://kishor-naik-dotnet.blogspot.in/2011/06/cnet-get-mac-address-through-cnet.html

The script runs only Internet Explorer browser and IE setting must allow creating ActiveX Object.

WMI Scripting Library
The WMI scripting library provides the set of automation objects through which scripting languages, such as VBScript, JScript, and ActiveState ActivePerl access the WMI infrastructure. The WMI scripting library is implemented in a single automation component named wbemdisp.dll that physically resides in the systemroot\System32\Wbem directory.

Let see how to get MAC address from Client machine.

Step 1
Install WMI Script Library on Client Machine,WMI Library download from the following Link
http://www.microsoft.com/downloads/details.aspx?FamilyID=6430f853-1120-48db-8cc5-f2abdc3ed314&displaylang=en

Note : For Window XP and Window Server 2003 only.

Step 2
Change the security setting of IE,it is look like this.
Go to Tools -> Internet Options -> Security -> Custom Level


  1. Initialize and script ActiveX controls not marked as safe for script -> Set it to enable or prompt.
  2. Run ActiveX controls and plugins -> Set it to enable or prompt.

Click on Image for better view


Click on Image for better view

Step3
Create a ASP.net empty web site and give the solution name as SolGetClientMAC_ASP.net.

Step 4
Download JQuery from Nuget Package Manager,Right click on Website and select Manage Nuget Packages,it is look like this


Click on Image for better view

Step 5
Add a Button control and Div tag on page,it is look like this
<form id="form1" runat="server">
    <div>
        <asp:Button ID="btnMACAddress" runat="server" Text="Get MAC Address" />

        <div id="GetMAC"></div>
    </div>
</form>

On Button client side click event to bind MAC address in DIV tag using Jquery.

Step 6
Add JQuery file Reference inside the head tag of the page,it is look like this
<script type="text/javascript" src="Scripts/jquery-2.0.3.js"></script>

Step 7
Add a following script to get MAC address of Client Machine with the help of WMI Library and JQuery,it is look like this
<script type="text/javascript">

        $(document).ready(function () {

      
            $("#<%=btnMACAddress.ClientID%>").click(function (e) {

                try {
                    var ActiveXobj = new ActiveXObject("WbemScripting.SWbemLocator");
                    var Con = ActiveXobj.ConnectServer(".");

                    var Query = Con.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
                    var eResult = new Enumerator(Query);

                    var Table = $("<Table border='2' cellPadding='3px' cellSpacing='1px' bgColor='#CCCCCC'></Table>").append("<Tr  bgColor='#EAEAEA'><Td>Caption</Td><Td>MAC</Td></Tr>");
                  
                    while (!eResult.atEnd()) {

                        eResult.moveNext();

                        var Data = eResult.item();

                        if (Data != null) {

                            Table.append("<Tr bgColor='#FFFFFF'><Td>"+Data.Caption+"</Td><Td>"+Data.MACAddress+"</Td></Tr>");
                        }
                    }

                    // Print MAC Address
                    $("#GetMAC").html(Table);

                    e.preventDefault();
                }
                catch (ex) {
                    alert(ex.message);
                }

            });

        });

    </script>

Full .ASPX Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MAC address of Client Machine</title>
    <script type="text/javascript" src="Scripts/jquery-2.0.3.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {

      
            $("#<%=btnMACAddress.ClientID%>").click(function (e) {

                try {
                    var ActiveXobj = new ActiveXObject("WbemScripting.SWbemLocator");
                    var Con = ActiveXobj.ConnectServer(".");

                    var Query = Con.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
                    var eResult = new Enumerator(Query);

                    var Table = $("<Table border='2' cellPadding='3px' cellSpacing='1px' bgColor='#CCCCCC'></Table>").append("<Tr  bgColor='#EAEAEA'><Td>Caption</Td><Td>MAC</Td></Tr>");
                  
                    while (!eResult.atEnd()) {

                        eResult.moveNext();

                        var Data = eResult.item();

                        if (Data != null) {

                            Table.append("<Tr bgColor='#FFFFFF'><Td>"+Data.Caption+"</Td><Td>"+Data.MACAddress+"</Td></Tr>");
                        }
                    }

                    // Print MAC Address
                    $("#GetMAC").html(Table);

                    e.preventDefault();
                }
                catch (ex) {
                    alert(ex.message);
                }

            });

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnMACAddress" runat="server" Text="Get MAC Address" />

        <div id="GetMAC"></div>
    </div>
    </form>
</body>
</html>

Run the Project.

Output



Click on Image for better view

Note : I hide MAC address for security reasons. Reader must be run the solution and view their Machine MAC address.

Download
Download Source Code

No comments:

Post a Comment