日期:2014-05-18  浏览次数:20418 次

asp.net获取客户端的MAC地址
急求帮助!谢谢

------解决方案--------------------
获取服务器的IP地址方法以DNS法较为简单实用,如下:

private void ButtonIP_Click(object sender, System.EventArgs e)
{ Syste m.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 = "没有可用的连接";}}
 

  获取服务器的IP地址与MAC地址另一方法如下:

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



  获取客户端本机的IP地址相当简易,方法如下:

using System.Net;TextIP.Text=Page.Request.UserHostAddress;
 

asp.net获得客户端mac地址

#region 获得客户端mac地址
public string GetCustomerMac(string IP) //para IP is the client's IP
{
if (IP == "127.0.0.1")
{
return GetLocalMac();
}//"00-04-61-5C-31-52";//00-0F-1F-C6-B2-B3 
else
{
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", "");
int i = dirResults.LastIndexOf("=");
dirResults = dirResults.Substring(i + 2, 17);
if (dirResults.IndexOf("本地连接") != -1)
{ dirResults = "没有得到mac"; }
return dirResults;
}
}
#endregion


#region 获取客户端ip
public string ip()
{
return System.Web.HttpContext.Current.Request.UserHostAddress;
}
#endregion
#region 获得服务器端mac地址
public static string GetLocalMac()
{
ManagementClass adapters = new ManagementClass("Win32_NetworkAdapterConfiguration");
string MACAddress = "unknown";
foreach (ManagementObject adapter in adapters.GetInstances())
{
if ((bool)adapter["IPEnabled"] == true)
{
MACAddress = adapter.Properties["MACAddress"].Value.ToString();
break;
}
}
return MACAddress.Replace(":", "-");
}
#endregion