Obtain the NIC or IP address

Source: Internet
Author: User
Tags servervariables

Client ip:
Request. ServerVariables. Get ("Remote_Addr"). ToString ();
Client Host Name:
Request. ServerVariables. Get ("Remote_Host"). ToString ();
Client Browser IE:
Request. Browser. Browser;
Client browser version:
Request. Browser. MajorVersion;
Client operating system:
Request. Browser. Platform;
Server ip Address:
Request. ServerVariables. Get ("Local_Addr"). ToString ();
Server Name:
Request. ServerVariables. Get ("Server_Name"). ToString ();
If you want to learn more about ServerVariables, you can use
Foreach (String o in Request. ServerVariables ){
Response. Write (o + "=" + Request. ServerVariables [o] + "<br> ");
}
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 (); // obtain the NIC address
String [] IPAddresses = (string []) MO ["IPAddress"]; // obtain the local IP Address
If (IPAddresses. Length> 0)
StringIP = IPAddresses [0];
Response. Write (stringMAC + "/" + stringIP );
}
}
Asp.net + c # How do I obtain the MAC address of the client Nic?
// Reference to the following two namespaces
Using System. Diagnostics;
Using System. Text. RegularExpressions;
// Obtain the NIC physical address (MAC) of the remote client)
Public string GetMac (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 "";
}
}
}
// Print the client's Nic physical address (MAC) on the page)
Response. Write (this. GetMac (Request. UserHostAddress. ToString ()));
Obtain the cpu serial number, hard disk ID, and nic MAC address.
Private void GetInfo ()
{
String cpuInfo = ""; // cpu serial number
ManagementClass cimobject = new ManagementClass ("Win32_Processor ");
ManagementObjectCollection moc = cimobject. GetInstances ();
Foreach (ManagementObject mo in moc)
{
CpuInfo = mo. Properties ["ProcessorId"]. Value. ToString ();
Response. Write ("cpu serial number:" + cpuInfo. ToString ());
}
// Obtain the hard disk ID
String HDid;
ManagementClass cimobject1 = new ManagementClass ("Win32_DiskDrive ");
ManagementObjectCollection moc1 = cimobject1.GetInstances ();
Foreach (ManagementObject mo in moc1)
{
HDid = (string) mo. Properties ["Model"]. Value;
Response. Write ("hard disk serial number:" + HDid. ToString ());
}

// Obtain the hardware address of the NIC
ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc2 = mc. GetInstances ();
Foreach (ManagementObject mo in moc2)
{
If (bool) mo ["IPEnabled"] = true)
Response. Write ("MAC address \ t {0}" + mo ["MacAddress"]. ToString ());
Mo. Dispose ();
}
}

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.