Local MAC address of the machine, Client IP address, MAC address method (source code)

Source: Internet
Author: User
Tags servervariables
The complete code is as follows. If you have any need, refer to Luo.
Obtain the MAC address of the local machine.
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Management;
 

Public partial class Mac: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String stringmac = ""; string stringip = ""; managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (bool) Mo ["ipenabled"] = true)
{
Stringmac + = Mo ["macaddress"]. tostring ();
Textbox2.text = stringmac. tostring ();
String [] ipaddresses = (string []) Mo ["IPaddress"];
If (ipaddresses. length> 0)
Stringip = ipaddresses [0];
Textbox2.text = textbox2.text + "&" + stringip. tostring ();
}
}
Response. Write (stringmac. tostring () + "<br> ");
Response. Write (stringip. tostring ());
}
}

How to obtain the Client IP address and MAC address in the bsstructure

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Text. regularexpressions;
Using system. net;
Using system. diagnostics;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Ip
System. net. IPaddress IP = system. net. IPaddress. parse (request. userhostaddress );
System. net. iphostentry Ihe = DNS. gethostentry (IP );
// Machine name
String clientname = IHE. hostname;
Response. Write (IP. tostring ());
Response. Write ("<br> ");
Response. Write (clientname. tostring ());
Response. Write ("<br> ");
// Response. Write (request. servervariables ["http_x_forwarded_for"] [0]. tostring ());
Response. Write ("----------");
Response. Write (_ default. Ia. tostring ());
Response. Write ("<br> ");
Response. Write ("-------------------");
String I = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL
& Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty)
? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]
: Httpcontext. Current. Request. servervariables ["remote_addr"];
Response. Write (I );
Response. Write ("----------");
Response. Write (request. servervariables ["http_x_forwarded_for"]);
Response. Write (request. servervariables ["remote_addr"]);
Response. Write ("aaaaaaaaaaa ");
Response. Write (_ default. getcustomermac ("192.168.8.244 "));
}
Public static string IA
{
Get
{
String result = string. empty;

Result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
If (result! = NULL & result! = String. Empty)
{
// A proxy may exist.
If (result. indexof (".") =-1) // No "." is definitely not in IPv4 format.
Result = NULL;
Else
{
If (result. indexof (",")! =-1)
{
// There are ",", and multiple proxies are estimated. Obtain the first IP address that is not an intranet IP address.
Result = result. Replace ("", ""). Replace ("'","");
String [] temparyip = result. Split (",;". tochararray ());
For (INT I = 0; I <temparyip. length; I ++)
{
If (_ default. isipaddress (temparyip [I])
& Temparyip [I]. substring (0, 3 )! = "10 ."
& Temparyip [I]. substring (0, 7 )! = "192.168"
& Temparyip [I]. substring (0, 7 )! = "172.16 .")
{
Return temparyip [I]; // locate an address that is not an intranet address
}
}
}
Else if (_ default. isipaddress (result) // The proxy is in the IP Format.
Return result;
Else
Result = NULL; // the content in the proxy is not an IP address and the IP address is used.
}

}

String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL & httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty )? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]: httpcontext. Current. Request. servervariables ["remote_addr"];

If (null = Result | result = string. Empty)
Result = httpcontext. Current. Request. servervariables ["remote_addr"];

If (result = NULL | result = string. Empty)
Result = httpcontext. Current. Request. userhostaddress;

Return result;
}
}

Public static bool isipaddress (string str1)
{
If (str1 = NULL | str1 = string. Empty | str1.length <7 | str1.length> 15) return false;

String regformat = @ "^/d {1, 3} [/.] /d {1, 3} [/.] /d {1, 3} [/.] /d {1, 3} $ ";

RegEx = new RegEx (regformat, regexoptions. ignorecase );
Return RegEx. ismatch (str1 );
}

Public static string getcustomermac (string IP) // para IP is the client's IP
{
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 ();
Dirresults = dirresults. Replace ("/R", ""). Replace ("/N", ""). Replace ("/T ","");

RegEx Reg = new RegEx ("Mac [] {0,} Address [] {0,} = [] {0 ,}(? <Key> ((.)*?)) _ Mac ", regexoptions. ignorecase | regexoptions. Compiled );
Match MC = reg. Match (dirresults + "_ Mac ");

If (MC. Success)
{
Return MC. Groups ["key"]. value;
}
Else
{
Reg = new RegEx ("Host Not Found", regexoptions. ignorecase | regexoptions. Compiled );
MC = reg. Match (dirresults );
If (MC. Success)
{
Return "host not found! ";
}
Else
{
Return "";
}
}
}

}

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.