Asp.net get Mac (final)

Source: Internet
Author: User
Tags servervariables

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;
}
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.