在做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都是系統自帶,不用去下載或注冊。
<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) != "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;
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 "";
}
}
}
}
本文轉自:http://tb.blog.csdn.Net/TrackBack.ASPx?PostId=1327256