Using system;
Using system. Collections. Generic;
Using system. text;
Using system. net;
Using system. Text. regularexpressions;
Using system. diagnostics;
Using system. runtime. interopservices;
Using system. Management;
Using system. Management. instrumentation;
Using system. runtime. remoting. contexts;
Using system. Web;
Namespace comlib
{
Public class getip_mac
{
[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 );
Public static string getip ()
{
String IP = "";
If (system. Web. httpcontext. Current. Request. servervariables ["http_via"]! = NULL) // server, using proxy
{
// Obtain the real client address
IP = system. Web. httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring (); // return real client IP.
}
Else // if no proxy server is used or the client's IP address is not using proxy or can't get the client IP Address
{
// Obtain the server address
IP = system. web. httpcontext. current. request. servervariables ["remote_addr"]. tostring (); // while it can't get the client IP, it will return proxy IP.
}
Return IP;
}
/// <Summary>
/// Obtain the MAC address
/// </Summary>
/// <Returns> </returns>
Public static string getmac ()
{
Try
{
String userip = system. Web. httpcontext. Current. Request. userhostaddress;
String strclientip = system. Web. httpcontext. Current. 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 ");
String mac_dest = "";
// If (mac_src = "0 ")
//{
// If (userip = "127.0.0.1 ")
// Mac_dest = "accessing localhost! ";
// Else
// Mac_dest = "Welcome to friends from" + userip +! "+" <Br> ";
// Return mac_dest;
//}
While (mac_src.length <12)
{
Mac_src = mac_src.insert (0, "0 ");
}
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> ");
Return mac_dest;
}
Catch
{
Return "host information not found ";
}
}
}
}