Please disable your adblock and script blockers to view this page
 

How to read mac address using Asp.Net C#,VB ?

Introduction:  In previous articles i explained How to get IP Address of the client using asp.net  and Get city, state and country based on zip code using Googlemap API in asp.net and  Add meta description, meta keywords tags from code behind in asp.net and Get Title, Description and Keywords Meta tags from URL in asp.net  and  Turn off browser autocomplete feature in textbox in asp.net

DescriptionIn this article i have explained how to get/read/fetch MAC Address of the client using asp.net. It is sometimes required to read the MAC address of the client to know from where the visitors are coming to your website or for other purpose as per application requirement. 

Implementation: Let's understand by practical implementation.


Asp.Net C# Code to get/read mac address of the client

First of all include following namespaces:

using System;

using System.Management;

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

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) 
            {
                if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }

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

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

First of all import following namespaces:

imports System

imports System.Management

  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

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon