Jun 29, 2010

Get Mac Adress in asp.net

 using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;
using System.Text;
using System.Net;



public partial class macaddrees : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = FindMACAddress();
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        Response.Write("ip:"+clientIPAddress+"<br/>" );
        Response.Write("macaddress:"+ s.ToString());
         Response.Write("<br/>"+Request.UserHostAddress.ToString());
          Response.Write("<br/>"+Request.UserHostName.ToString()+"<br/>" );
         Response.Write("browser:"+Request.Browser.Browser.ToString());

         Response.Write("<br/>" + Request.Browser.MajorVersion.ToString()); ;
          Response.Write("<br/>"+Request.Browser.MinorVersion.ToString());
         Response.Write("<br/>"+Request.Browser.Platform.ToString());
      
    }

    public string FindMACAddress()
    {
        //create out management class object using the
        //Win32_NetworkAdapterConfiguration class to get the attributes
        //af the network adapter

        ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
        //create our ManagementObjectCollection to get the attributes with
        ManagementObjectCollection objCol = mgmt.GetInstances();
        string address = String.Empty;
        //loop through all the objects we find
        foreach (ManagementObject obj in objCol)
        {
            if (address == String.Empty)  // only return MAC Address from first card
            {
                //grab the value from the first network adapter we find
                //you can change the string to an array and get all
                //network adapters found as well
                if ((bool)obj["IPEnabled"] == true) address = obj["MacAddress"].ToString();
            }
            //dispose of our object
            obj.Dispose();
        }
        //replace the ":" with an empty space, this could also
        //be removed if you wish
        address = address.Replace(":", "");
        //return the mac address
        return address;
    }
}

o/p:
ip:192.168.11.2
macaddress:0017C4960693
127.0.0.1
127.0.0.1
browser:Firefox
3
0.6
WinXP



No comments:

Post a Comment