Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday, 6 February 2015

Introduction:

In this article i have explained how to get MAC Address using asp.net. It is sometimes required to read the MAC address of the user to know from where the visitors are coming to your website or for other purpose as per application requirement such as to restrict user of particular Mac Address or to provide access only to specific user with specific MAC address.



Implementation: Let's understand by practical implementation.

ASP.NET(C#) Code to get mac address of the client

    protected void Page_Load(object sender, EventArgs e)
    {
            string MacAddress = GetMACAddress();
     Response.Write("Mac addess is : " + MacAddress);
    }

public string GetMACAddress()
    {
        ManagementClass mc = newManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MACAddress = String.Empty;
        foreach (ManagementObject mo in moc)
        {
            if (MACAddress == String.Empty) 
            {
                if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }

        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }

VB.Net Code to get/read mac address of the client

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)Handles Me.Load
        Dim MacAddress As String = GetMACAddress()
        Response.Write("Mac addess is : " & MacAddress)
    End Sub

    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
                If CBool(mo("IPEnabled")) = True Then
                    MACAddress = mo("MacAddress").ToString()
                End If
            End If
            mo.Dispose()
        Next

        MACAddress = MACAddress.Replace(":", "")
        Return MACAddress

    End Function

0 comments:

Post a Comment