The web program obtains the MAC address of the client.

Source: Internet
Author: User

A few days ago, someone asked me how to get the MAC address of the client in ASP. NET. I didn't think much about it at the time. I gave him a piece of code, as shown below:

Code
[DllImport ("Iphlpapi. dll")]
Private static extern int SendARP (Int32 dest, Int32 host, ref Int64 mac, ref Int32 length );
[DllImport ("Ws2_32.dll")]
Private static extern Int32 inet_addr (string ip );

Private void GetMacByARP ()
{
Try
{
String userip = Request. UserHostAddress;
String strClientIP = Request. UserHostAddress. ToString (). Trim ();
Int32 ldest = inet_addr (strClientIP); // ip address of the destination
Int32 lhost = inet_addr (""); // ip address of the local server
Int64 macinfo = new Int64 ();
Int32 len = 6;
Int res = SendARP (ldest, 0, ref macinfo, ref len );
String mac_src = macinfo. ToString ("X ");
If (mac_src = "0 ")
{
If (userip = "127.0.0.1 ")
Response. Write ("accessing Localhost! ");
Else
Response. Write ("Welcome to friends from" + userip +! "+" <Br> ");
Return;
}

While (mac_src.Length <12)
{
Mac_src = mac_src.Insert (0, "0 ");
}

String mac_dest = "";

For (int I = 0; I <11; I ++)
{
If (0 = (I % 2 ))
{
If (I = 10)
{
Mac_dest = mac_dest.Insert (0, mac_src.Substring (I, 2 ));
}
Else
{
Mac_dest = "-" + mac_dest.Insert (0, mac_src.Substring (I, 2 ));
}
}
}

Response. Write ("Welcome to friends from" + userip + "<br>" + "and MAC address" + mac_dest +! "

+ "<Br> ");
}
Catch (Exception err)
{
Response. Write (err. Message );
}
}

Later, I told him that the local test was still usable. It would not work if I uploaded the data to an internet server. I checked it. Indeed, I used ARP packets to get the MAC address, it should not work in different network segments. Think about it. The safest way is to get a MAC on the client and send it to the server. This requires relying on javascript (I hate vbscript), and then google, the solution is found here:

Http://zhupan.javaeye.com/blog/26440

However, we can see that the provided code is a little messy and inconvenient to call. As a result, we have transformed the Code as follows:

Code
Document. write ("<OBJECT id = 'locator' classid = 'clsid: 76A64158-CB41-11D1-8B02-00600806D9B6 'VIEWASTEXT> </OBJECT> ");
Document. write ("<OBJECT id = foo classid = CLSID: 75718C9A-F029-11d1-A1AC-00C04FB6C223> </OBJECT> ");
Var MACAddr, IPAddr, sDNSName
Function getObject (objObject, objAsyncContext ){
If (objObject. MACAddress! = Null & objObject. MACAddress! = "Undefined ")
MACAddr = objObject. MACAddress;
If (objObject. IPEnabled & objObject. IPAddress (0 )! = Null & objObject. IPAddress (0 )! = "Undefined ")
IPAddr = objObject. IPAddress (0 );
If (objObject. DNSHostName! = Null & objObject. DNSHostName! = "Undefined ")
SDNSName = objObject. DNSHostName;
}
Function setValue (hResult, pErrorObject, pAsyncContext ){
CreateTxt ("txtMAC", MACAddr );
CreateTxt ("txtIp", IPAddr );
CreateTxt ("txtPCName", sDNSName );
}
Function createTxt (txtName, txtValue ){
Var macTxt = document. createElement ("INPUT ");
MacTxt. name = txtName;
MacTxt. value = txtValue;
MacTxt. type = "hidden ";
Document. forms [0]. appendChild (macTxt );
}
Document. getElementById ("foo"). attachEvent ("OnObjectReady", getObject );
Document. getElementById ("foo"). attachEvent ("OnCompleted", setValue );

Var service = locator. ConnectServer ();
Var MACAddr;
Var IPAddr;
Var DomainAddr;
Var sDNSName;
Service. Security _. ImpersonationLevel = 3;
Service. InstancesOfAsync (foo, 'win32 _ networkadapterconfiguration ');

Save this JS file to the js file, and then directly reference it on the Web page without calling a function. When the server uses the MAC address: Request. Form ["txtMAC"]. The txtIp and txtPCName values are provided. Note: you may need to set the IE permission.

We can also use attachEvent to specify the event processor for the event of component objects called with objects in the script tag, if you need to pass parameters in the event, you only need to ensure that the input parameter order is consistent with the output parameter. (refer to the section about arguments in javascript advanced programming ).

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.