Asp.net obtains the Client IP address and mac address

Source: Internet
Author: User

Obtain the Client IP Address:

Reference content is as follows:

Private string GetClientIP ()
{
String result = HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"];
If (null = result | result = String. Empty)
{
Result = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];
}

If (null = result | result = String. Empty)
{
Result = HttpContext. Current. Request. UserHostAddress;
}
Return result;
}

Obtain the MAC address:

Reference content is as follows:

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

Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
Try
{
String userip = Request. UserHostAddress;
String strClientIP = 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 ");
If (mac_src = "0 ")
{
If (userip = "127.0.0.1 ")
Response. Write ("accessing Localhost! ");
Else
Response. Write ("Welcome to friends from" + userip +! "+"
");
Return;
}

While (mac_src.Length <12)
{
Mac_src = mac_src.Insert (0, "0 ");
}

String mac_dest = "";

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 ));
}
}
}

// Method 2
Using System. Text. RegularExpressions;
Using System. Diagnostics;
Public class test
{
Public test
{}
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 ,}(? ((.)*?)) _ 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 "";
}
}
}
}

This method has to be explored in some places, otherwise it cannot be understood.

 

//

The DNS method is simple and practical to obtain the IP address of a server, as follows:
Using System. Net;

Private void ButtonIP_Click (object sender, System. EventArgs e)
{
System. Net. IPAddress [] addressList = Dns. GetHostByName (Dns. GetHostName (). AddressList;
If (addressList. Length> 1)
{TextLIP. Text = addressList [0]. ToString ();
TextSIP. Text = addressList [1]. ToString ();
}
Else
{
TextLIP. Text = addressList [0]. ToString ();
TextSIP. Text = "no available connections ";
}
}

Another method to obtain the Server IP address and MAC address is as follows:

Reference content is as follows:

Using System. Management;

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 ();
TextMAC. Text = stringMAC. ToString ();
String [] IPAddresses = (string []) MO ["IPAddress"];
If (IPAddresses. Length> 0)
StringIP = IPAddresses [0];
TextIP. Text = stringIP. ToString ();
}
}

It is quite easy to obtain the IP address of the local client. The method is as follows:

Reference content is as follows:

Using System. Net;

TextIP. Text = Page. Request. UserHostAddress;

To obtain the MAC address of the client's local machine, it is more complicated to import and call two APIs and use ARP to obtain the MAC address of the machine in the same network segment, for cross-network profit, use the IP scan or the nbtstat command in cmd to obtain the MAC address. You can also read the system registry value or WMI database.

 

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.