ASP. NET (C #) obtain the MAC code of the client Nic
In the past two days, I have searched for information about obtaining the client Nic on the Internet, which is almost the same on the Internet ~ I tried a lot of code ~, I sorted it out yesterday and sent the code. I hope it will help you ~
1. First add reference:
"Project" -- "add reference" --- "using System. Runtime. InteropServices ;"
2. 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 Page_Load (object sender, System. EventArgs e)
{
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 );
}
}
--------------------------
If you have time, I will send this project file ~, Okay. Let's write this today ~