Obtain the MAC address of the client

Source: Internet
Author: User
To obtain the MAC of the client, the web server must be in the same lan network segment. Since it is already in the same network segment, the subnet mask and gateway are the same; therefore, you can only obtain the Client IP to Mac conversion process. If the lan mac has registration information, you can determine the user access record.
<% @ Page Language = "C #" DEBUG = "true" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<% @ Import namespace = "system. runtime. interopservices" %>
<Script language = "C #" runat = "server">
[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 );
Void page_load (Object sender, eventargs E)
{
Try
{
String userip = request. userhostaddress;
Int32 ldest = inet_addr (userip); // 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 ")
Userinfo. Text = "accessing localhost! ";
Else
Userinfo. Text = "Welcome to friends from" + userip +! ";
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 ))
Mac_dest = mac_dest.insert (0, mac_src.substring (I, 2 ));
}
Userinfo. Text = "Welcome to friends from" + userip + "and" + mac_dest + "for MAC address! ";
}
Catch (exception ERR)
{
Response. Write (ERR. Message );
}
}
</SCRIPT>
<Htm>
<Body>
<Center>
<Asp: Label id = "userinfo" runat = Server> </ASP: Label>
</Center>
</Body>
</Htm>

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.