在做B/S结构的系统时,我们常常需要获取客户端的一些信息,如IP和MAC,以结合身份验证。在ASP.NET中,要获取服务器端的MAC很容易,但是要获取客户端的MAC的地址确要花费一翻心思,通常的做法是调用Win32API或直接调用nbtstat命令。
方法一:Javascript结合系统的ActiveX
优点:无需开发额外的代码,轻量级的实现。不需要服务器端进行处理,有客户端自行获取,传递到服务器端,且速度和可靠性都比在服务器端获取好
缺点:受客服端平台的限制,如安全级别设置较高,则无常正常执行
具体实现的html和javascript如下:
其实最关键的还是用到两个ActiveX:
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
不过这两个ActiveX都是系统自带,不用去下载或注册。
< 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 ) != " undefined " )
IPAddr = objObject.IPAddress( 0 );
if (objObject.DNSHostName != null && objObject.DNSHostName != " undefined " )
sDNSName = objObject.DNSHostName;
}
</ SCRIPT >
< META content ="MSHTML 6.00.2800.1106" name =GENERATOR ></ HEAD >
< 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 =txtMACAddr > < INPUT value =192.168.0.2 name =txtIPAddr > < INPUT value =typ name =txtDNSName > </ FORM ></ BODY ></ HTML >
方法二:通过插件调用Win32API
优点:成功率较高,不受客户浏览器的限制。
缺点:需客户端安装此插件,且只能是Windows系统
具体实现这里不做说明,不同平台和语言实现方法各不相同,以上只是举例Windows系统。
方法三:通过后台进程调用nbtstat命令
优点:无需额外开发组件,不受客户端平台的限制
缺点:受网络范围的限制,成功率较低,速度慢。
具体实现原理:通过nbtstat -a IP 命令将对方计算机的信息输出管道,然后捕获管道输出的内容,再通过正则表达式将MAC筛选出来。
C#代码如下:
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
public static class CustomerMAC
{
/// <summary>
/// 根据IP获取客户端网卡的MAC
/// </summary>
/// <param name="IP">客户端IP</param>
/// <returns>网卡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 = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
mc = reg.Match(dirResults);
if (mc.Success)
{
return "Host not found!";
}
else
{
return "";
}
}
}
}