ASP. NET obtains the MAC address used by the client Nic.

Source: Internet
Author: User

Method 1: Use the class library that comes with. Net to directly obtain

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> // Add a reference and import the namespace <br/> using system. management; <br/> namespace webapp <br/>{< br/> Public partial class webform1: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> response. write ("computer name:" + system. net. DNS. gethostname () + "<br>"); <br/> // call the getmacaddress function to obtain the NIC address information. <br/> response. write ("MAC address:" + getmacaddress ()); <br/>}< br/> /// <summary> <br/> // obtain the NIC address information <br/> /// </Summary> <br/ >/// <returns> </returns> <br/> string getmacaddress () <br/>{< br/> try <br/> {<br/> string MAC = ""; <br/> managementclass MC = new managementclass ("win32_networkadapterconfiguration "); <br/> managementobjectcollection MOC = MC. getinstances (); <br/> foreach (managementobject Mo in MoC) <br/>{< br/> If (bool) Mo ["ipenabled"] = true) <br/> {<br/> MAC = Mo ["macaddress"]. tostring (); <br/> break; <br/>}< br/> MOC = NULL; <br/> MC = NULL; <br/> return MAC; <br/>}< br/> catch <br/> {<br/> return "unknow "; <br/>}< br/> 

Running result:
Name: lenovo-05d886ff
MAC address: 00: 26: 22: 05: 8c: C7

Note: After uploading to the server for testing, we find that the MAC address of the server is obtained.


Method 2: obtained through the Win32 feature
Advantage: you do not need to set it on the client;
Disadvantage: the MAC of the server cannot be obtained (it is tested to be 0, but it does not feel necessary to obtain the MAC of the server, so it is not a disadvantage) (whether cross-network segment retrieval is not tested yet)

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> // import the namespace <br/> using system. runtime. interopservices; <br/> namespace webapp <br/>{< br/> Public partial class webform2: system. web. UI. page <br/> {<br/> [dllimport ("iphlpapi. DLL ")] <br/> Private Static extern int sendarp (int32 DEST, Int32 host, ref int64 Mac, ref int32 length); <br/> [dllimport ("ws2_32.dll")] <br/> Private Static extern int32 inet_addr (string IP ); <br/> private void page_load (Object sender, system. eventargs e) <br/>{< br/> try <br/>{< br/> string userip = request. userhostaddress; <br/> string strclientip = request. userhostaddress. tostring (). trim (); <br/> int32 ldest = inet_addr (strclientip); // ip address of the destination <br/> Int64 macinfo = new int64 (); <br/> int32 Len = 6; <br/> int res = sendarp (ldest, 0, ref macinfo, ref Len ); <br/> string mac_src = macinfo. tostring ("X"); <br/> If (mac_src = "0") <br/> {<br/> If (userip = "127.0.0.1 ") <br/> {<br/> response. write ("accessing localhost! "); <Br/>}< br/> else <br/> {<br/> response. Write (" Welcome to friends from "+ userip +! "+" <Br> "); <br/>}< br/> return; <br/>}< br/> while (mac_src.length <12) <br/>{< br/> mac_src = mac_src.insert (0, "0"); <br/>}< br/> string mac_dest = ""; <br/> for (INT I = 0; I <11; I ++) <br/>{< br/> If (0 = (I % 2 )) <br/>{< br/> if (I = 10) <br/>{< br/> mac_dest = mac_dest.insert (0, mac_src.substring (I, 2 )); <br/>}< br/> else <br/> {<br/> mac_dest = "-" + mac_dest.insert (0, mac_src. Substring (I, 2); <br/>}< br/> response. write ("Welcome to" + userip + "<br>" + ", and" + mac_dest + "for MAC address! "+" <Br> "); <br/>}< br/> catch (exception ERR) <br/>{< br/> response. write (err. message); <br/>}< br/> 

Running result:
Accessing localhost!

Note: After uploading to the server for testing, we find that the MAC address of the server is obtained.

Method 3: Load ActiveX using JavaScript:

Advantage: Any MAC address can be obtained across network segments );

Disadvantage: You need to open all ActiveX options in the client browser; otherwise, the options cannot be obtained, so they are not stable;

Front-end code: (webform3.aspx)

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "webform3.aspx. cs" inherits = "webapp. webform3" %> <br/> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead runat = "server"> <br/> <title> </title> <br/> <MCE: script Language = "JavaScript"> <! -- <Br/> document. write ("<Object ID = 'locate' classid = 'clsid: 76a64158-cb41-11d1-8b02-00600806d9b6 'viewastext> </Object>"); <br/> document. write ("<Object ID = Foo classid = CLSID: credential> </Object>"); <br/> var macaddr, ipaddr, sdnsname <br/> function GetObject (objobject, objasynccontext) {<br/> try {<br/> If (macaddr = NULL & objobject. ipenabled) {<br/> If (objobject. ma Caddress! = NULL & objobject. macaddress! = "Undefined") <br/> macaddr = objobject. macaddress; <br/> If (objobject. ipenabled & objobject. IPaddress (0 )! = NULL & objobject. IPaddress (0 )! = "Undefined") <br/> ipaddr = objobject. IPaddress (0); <br/> If (objobject. dnshostname! = NULL & objobject. dnshostname! = "Undefined") <br/> sdnsname = objobject. dnshostname; <br/>}< br/> catch (ERR) <br/>{}< br/>}< br/> function setvalue (hresult, perrorobject, pasynccontext) {<br/> createtxt ("txtmac", macaddr); <br/> createtxt ("txtip", ipaddr ); <br/> createtxt ("txtpcname", sdnsname); <br/>}< br/> function createtxt (txtname, txtvalue) {<br/> var mactxt = document. createelement ("input"); <br/> mactxt. name = txtname; <br/> mactxt. value = txtvalue; <br/> mactxt. type = "hidden"; <br/> try {<br/> document. forms [0]. appendchild (mactxt); <br/>}< br/> catch (ERR) {<br/> // retrieval failed <br/>}< br/> document. getelementbyid ("foo "). attachevent ("onobjectready", GetObject); <br/> document. getelementbyid ("foo "). attachevent ("oncompleted", setvalue); <br/> try {<br/> var service = locator. connectserver (); <br/> var macaddr; <br/> var ipaddr; <br/> var domainaddr; <br/> var sdnsname; <br/> service. security _. impersonationlevel = 3; <br/> service. instancesofasync (Foo, 'win32 _ networkadapterconfiguration'); <br/>}< br/> catch (ERR) {<br/> // retrieval failed <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <Form ID = "form1" runat = "server"> <br/> <Div> </P> <p> <asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/> </P> <p> </div> <br/> </ form> <br/> </body> <br/> </ptml> <br/> 

Background Code: (webform3.aspx. CS)

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> namespace webapp <br/>{< br/> Public partial class webform3: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{< br/>}< br/> /// <summary> <br/> // obtain the MAC address <br/> /// </Summary> <br/> // <returns> </returns> <br/> Public String getmacs () <br/>{< br/> try <br/>{< br/> return request. form ["txtmac"]. tostring (); <br/>}< br/> catch <br/> {<br/> return ""; <br/>}< br/> protected void button#click (Object sender, eventargs e) <br/>{< br/> response. write (getmacs (); <br/>}< br/> 

Running prompt:

 

Enable ActiveX that is not marked as a secure script
Step 1: Tools> Internet Options
Step 2: Security-> Internet-> Custom Level
Step 3: Enable "initialize and execute ActiveX controls that are not marked as secure scripts", as shown in:

Running result:

 

Conclusion: The first method is convenient. The second method cannot obtain the MAC address of the server. The third method still needs to be set ".

 

 

Reference: http://www.xueit.com/asp.net/show-6092-1.aspx

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.