想找客戶端唯一標識,可是沒找到結果,不過找到了其它一些代碼。
1、得到服務器MAC地址
2、得到局域網內MAC方法1
3、得到局域網內MAC方法2
4、得到局域網內MAC方法3
1、得到服務器MAC地址
/// <summary>
/// 得到服務器MAC地址
/// </summary>
void GetClientMacMethodTwo()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
Response.Write("MAC address\t\r" + mo["MacAddress"].ToString() + "");
}
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
Response.Write("Volume Serial Number: " + disk.GetPropertyValue("VolumeSerialNumber").ToString());
}
/// 得到服務器MAC地址
/// </summary>
void GetClientMacMethodTwo()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
Response.Write("MAC address\t\r" + mo["MacAddress"].ToString() + "");
}
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
Response.Write("Volume Serial Number: " + disk.GetPropertyValue("VolumeSerialNumber").ToString());
}
2、得到局域網內MAC方法1
[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);
/// <summary>
/// 得到局域網內MAC
/// </summary>
void GetClientMacMehtod()
{
// 在此处放置用户代码以初始化页面
try
{
string userip = Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString().Trim();
Int32 ldest = inet_addr(strClientIP); //目的地的ip
Int32 lhost = inet_addr(""); //本地服务器的ip
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("正在访问Localhost!");
else
Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
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));
}
}
}
Response.Write("欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!"
+ "<br>");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
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);
/// <summary>
/// 得到局域網內MAC
/// </summary>
void GetClientMacMehtod()
{
// 在此处放置用户代码以初始化页面
try
{
string userip = Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString().Trim();
Int32 ldest = inet_addr(strClientIP); //目的地的ip
Int32 lhost = inet_addr(""); //本地服务器的ip
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("正在访问Localhost!");
else
Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
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));
}
}
}
Response.Write("欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!"
+ "<br>");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
3、得到局域網內MAC方法2
/// <summary>
/// 局域網內MaC
/// </summary>
/// <param name="A_strIP"></param>
/// <returns></returns>
public string GetMACFromIP(string A_strIP)
{
string strRet = "Unknown";
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";
Regex objRex = new Regex(strIPPattern);
if (objRex.IsMatch(A_strIP) == true)
{
Int32 intDest = inet_addr(A_strIP);
Int32[] arrMAC = new Int32[2];
Int32 intLen = 6;
int intResult = SendARP(intDest, 0, ref arrMAC[0], ref intLen);
if (intResult == 0)
{
Byte[] arrbyte = new Byte[8];
arrbyte[5] = (Byte)(arrMAC[1] >> 8);
arrbyte[4] = (Byte)arrMAC[1];
arrbyte[3] = (Byte)(arrMAC[0] >> 24);
arrbyte[2] = (Byte)(arrMAC[0] >> 16);
arrbyte[1] = (Byte)(arrMAC[0] >> 8);
arrbyte[0] = (Byte)arrMAC[0];
StringBuilder strbMAC = new StringBuilder();
for (int intIndex = 0; intIndex < 6; intIndex++)
{
if (intIndex > 0) strbMAC.Append("-");
strbMAC.Append(arrbyte[intIndex].ToString("X2"));
}
strRet = strbMAC.ToString();
}
}
return strRet;
}
/// 局域網內MaC
/// </summary>
/// <param name="A_strIP"></param>
/// <returns></returns>
public string GetMACFromIP(string A_strIP)
{
string strRet = "Unknown";
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";
Regex objRex = new Regex(strIPPattern);
if (objRex.IsMatch(A_strIP) == true)
{
Int32 intDest = inet_addr(A_strIP);
Int32[] arrMAC = new Int32[2];
Int32 intLen = 6;
int intResult = SendARP(intDest, 0, ref arrMAC[0], ref intLen);
if (intResult == 0)
{
Byte[] arrbyte = new Byte[8];
arrbyte[5] = (Byte)(arrMAC[1] >> 8);
arrbyte[4] = (Byte)arrMAC[1];
arrbyte[3] = (Byte)(arrMAC[0] >> 24);
arrbyte[2] = (Byte)(arrMAC[0] >> 16);
arrbyte[1] = (Byte)(arrMAC[0] >> 8);
arrbyte[0] = (Byte)arrMAC[0];
StringBuilder strbMAC = new StringBuilder();
for (int intIndex = 0; intIndex < 6; intIndex++)
{
if (intIndex > 0) strbMAC.Append("-");
strbMAC.Append(arrbyte[intIndex].ToString("X2"));
}
strRet = strbMAC.ToString();
}
}
return strRet;
}
4、得到局域網內MAC方法3
/// <summary>
/// 局域網內MAC
/// </summary>
/// <param name="IP"></param>
/// <returns></returns>
public string GetCustomerMac(string IP)
{
if (IP == "127.0.0.1")
{
return "";
}
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;
}
}
/// 局域網內MAC
/// </summary>
/// <param name="IP"></param>
/// <returns></returns>
public string GetCustomerMac(string IP)
{
if (IP == "127.0.0.1")
{
return "";
}
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;
}
}