After testing, you can only obtain the same network segment ~
By the way
Asp.net: Obtain the hard disk ID of the server. Nic MAC address CPU serial number
Regular matching of multiple IP addresses, ports, and MAC addresses
JS get MAC address (continued)
Remember to reference using system. runtime. interopservices;
[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 );
/// <Summary>
/// Obtain the Client IP Address
/// </Summary>
/// <Returns> ip </returns>
Private string getclientip ()
{
String result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
If (null = Result | result = string. Empty)
{
Result = httpcontext. Current. Request. servervariables ["remote_addr"];
}
If (null = Result | result = string. Empty)
{
Result = httpcontext. Current. Request. userhostaddress;
}
Return result;
}
/// <Summary>
/// Obtain the MAC address of the client using the Client IP Address
/// </Summary>
/// <Param name = "remoteip"> Client IP </param>
/// <Returns> int16 Mac </returns>
Static private int64 getremotemac (string remoteip)
{
Int32 ldest = inet_addr (remoteip );
Try
{
Int64 macinfo = new int64 ();
Int32 Len = 6;
Int res = sendarp (ldest, 0, ref macinfo, ref Len );
Return macinfo;
}
Catch (exception ERR)
{
Console. writeline ("error: {0}", Err. Message );
}
Return 0;
}
/// <Summary>
/// Convert an int64 Mac to a correct client Mac
/// </Summary>
/// <Returns> Mac </returns>
Private string getclientmac ()
{
Int64 macid = getremotemac (getclientip ());
If (macid = 0)
Return "0 ";
String beforemacaddr = convert. tostring (macid, 16 );
String endmacaddr = "";
String [] macarray = new string [6];
For (INT I = 0; I <6; I ++)
{
Macarray [I] = beforemacaddr. substring (I * 2, 2 );
}
For (INT I = 0; I <6; I ++)
{
Endmacaddr + = macarray [5-I] + "-";
}
Endmacaddr = endmacaddr. substring (0, endmacaddr. Length-1 );
Endmacaddr = endmacaddr. toupper ();
Return endmacaddr;
}