How to obtain the MAC address of the client NIC

Source: Internet
Author: User
Tags static class
In the B/s structure of the system, we often need to obtain some information on the client, such as IP and MAC, in conjunction with authentication. In ASP.net, it is easy to get a server-side Mac, but it takes a lot of effort to get the MAC address of the client, usually by calling Win32API or calling the nbtstat command directly.

Method One: JavaScript combined with system ActiveX

Benefits: No need to develop additional code, lightweight implementations. Do not need server-side processing, have the client to obtain, passed to the server side, and speed and reliability than on the server to get better
Disadvantage: limited by the customer service platform, if the security level is set high, then the normal execution of impermanence

The concrete implementations of HTML and JavaScript are as follows:
In fact, the key is to use two ActiveX:
<object Id=locator Classid=clsid:76a64158-cb41-11d1-8b02-00600806d9b6 viewastext></object>
<object Id=foo classid=clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223></object>
However, these two ActiveX systems are self-contained and do not need to be downloaded or registered. < HTML >< Head >< title > WMI scripting HTML </title >
< META http-equiv =content-type Content = "text/html; charset=gb2312 ">
< SCRIPT language =jscript event = "oncompleted (hresult,perrorobject, Pasynccontext)" for =foo >
document.forms[0].txtmacaddr.value = unescape (MACADDR);
document.forms[0].txtipaddr.value = unescape (ipaddr);
document.forms[0].txtdnsname.value = unescape (sdnsname);
Document.formbar.submit ();
</SCRIPT >

< SCRIPT language =jscript event =onobjectready (objobject,objasynccontext) for =foo >
if (objobject.ipenabled!= null && objobject.ipenabled!= "undefined" && objobject.ipenabled = = True)
{
if (objobject.macaddress!= null && objobject.macaddress!= "undefined")
MACADDR = objobject.macaddress;
if (objobject.ipenabled && objobject.ipaddress (0)!= null && objobject.ipaddress (0)!= "un Defined ")
ipaddr = objobject.ipaddress (0);
if (objobject.dnshostname!= null && objobject.dnshostname!= "undefined")
Sdnsname = Objobject.dnshostname;
}
</SCRIPT >

< META content = "MSHTML 6.00.2800.1106" name =generator >< BODY >
< object ID =locator classid =clsid:76a64158-cb41-11d1-8b02-00600806d9b6 viewastext ></object >
< object ID =foo classid =clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223 ></object >
< SCRIPT language =jscript >
var service = Locator. ConnectServer ();
var macaddr;
var ipaddr;
var domainaddr;
var sdnsname;
Service. Security_. ImpersonationLevel = 3;
Service. Instancesofasync (foo, ' Win32_NetworkAdapterConfiguration ');
</SCRIPT >

< FORM ID =formfoo name =formbar action =nicpost.asp method =post >< INPUT value =00:05:5D:0E:C7:FA name =t Xtmacaddr > < INPUT value =192.168.0.2 name =txtipaddr > < INPUT value =typ name =txtdnsname > &L t;/ FORM ></body ></HTML >

Method Two: Invoke Win32API through the plug-in

Advantages: High success rate, not limited by customer browser.
Disadvantage: Require the client to install this plugin, and only the Windows system

Concrete implementation here does not explain, different platform and language implementation methods are not the same, the above is just an example of Windows system.

Method Three: Invoke the nbtstat command through a background process

Benefits: No additional development components, no client platform limitations
Disadvantages: Limited By the network scope, the success rate is low, the speed is slow.

The principle of implementation: through the nbtstat-a IP command to the other computer's information output pipeline, and then capture the contents of the pipeline output, and then through the regular expression of the Mac filter out.

The C # code is as follows:
Using System;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Diagnostics;

public static Class Customermac
{
<summary>
Get the Mac of the client network card based on IP
</summary>
<param name= "IP" > Client ip</param>
<returns> Nic Mac</returns>
public static string Getcustomermac (String 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 ("", ""). Replace ("", ""). Replace ("", "");

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 =</

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.