Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Advanced Technologies - Getting a MAC Address with ASP.NET 4.0 and C#
Getting a MAC Address with ASP.NET 4.0 and C#

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Advanced Technologies Tutorial

This tutorial will demonstrate how you can get a users MAC address using ASP.NET 4.0 and C#.

Adding the Default.aspx Page
To view the MAC address we will be getting we will need a simple web site that will allow us to display some data. At this point, I have created a new ASP.NET Empty Web Site and need to add in a Web Form with a label. To do this:
  1. Right click the project in your Solution Explorer.
  2. Select Add New Item...
  3. Select a Web Form.
  4. Name it Default.aspx.
  5. Click Add.
  6. Open Default.aspx up to Design mode.
  7. Drag and drop a Label onto the Web Form.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Getting the MAC Address in C#
Next, we need to add some code that will grab the MAC address from the current computer and display it on the web page. To do this:
  1. Open Default.aspx.cs up for editing.
  2. At the top of the class add the following using statement:
    using System.Net.NetworkInformation;
  3. In the Page_Load event method add in the following code:
    protected void Page_Load(object sender, EventArgs e)
    {
        //get all nics
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        //display the physical address of the first nic in the array,
        //which should correspond to our mac address
        Label1.Text = nics[0].GetPhysicalAddress().ToString();
    }

I just signed up at Server Intellect and couldn't be more pleased with my fully scalable and redundant cloud hosting! Check it out and see for yourself.

Let's review what this code is actually doing. First, we create an array of NetworkInterface objects and then populate that using the GetAllNetworkInterfaces method. Then, we display the physical or MAC address of the first NetworkInterface in the array which will correspond to your MAC address. Depending on your hardware you may want to use a different index of the array, however this will suffice for most machines.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks