C #. netObtain the IP address and MAC address of a computer
/// <Summary>
/// Obtain the user Client IP address (obtain the real IP address of the remote user through the proxy server)
/// </Summary>
Public static string getclientip ()
{
// If a proxy is used, obtain the real IP Address
String userip = string. empty;
If (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = "")
{
Userip = httpcontext. Current. Request. servervariables ["remote_addr"];
}
Else
{
Userip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
}
If (userip = NULL | userip = "")
{
Userip = httpcontext. Current. Request. userhostaddress;
}
Return userip;
// Httprequest request = httpcontext. Current. request;
// Try
//{
// If (httpcontext. Current. Request. servervariables ["http_via"]! = NULL)
//{
// Return httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring ();
//}
// Else
//{
// Return httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();
//}
//}
// Catch {return "127.0.0.1 ";}
}
// Obtain the MAC address
Public static string getcustomermac ()
{
String IP = getclientip ();
String dirresults = "";
Processstartinfo psi = new processstartinfo ();
Process proc = new process ();
PSI. filename = "NBTSTAT ";
PSI. redirectstandardinput = false;
PSI. redirectstandardoutput = true;
PSI. Arguments = "-a" + IP;
PSI. useshellexecute = false;
Proc = process. Start (PSI );
Dirresults = Proc. standardoutput. readtoend ();
Proc. waitforexit ();
// Match the MAC address
Match m = RegEx. match (dirresults, "\ W + \-\ W \ \ W ");
// If the match succeeds, the MAC address is returned; otherwise, the host information cannot be found.
If (M. tostring ()! = "")
{
Return M. tostring ();
}
Else
{
Return "host information not found ";
}
}