C #. net

Source: Internet
Author: User
Tags servervariables

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 ";

}

 

}

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.