Can I get a MAC address that are connected to my site.
this code get mac address host and return error permission.
String macadress = string.Empty;
开发者_C百科foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
OperationalStatus ot = nic.OperationalStatus;
if (nic.OperationalStatus == OperationalStatus.Up)
{
macadress = nic.GetPhysicalAddress().ToString();
break;
}
}
return macadress;
now how can get mac address users???
2. how can get ip users???
Unfortunately you can't get the user's MAC address in the way that you want. It's my understanding that MAC addresses are stripped from packets when they leave your local network.
You can try to get the user's address from Request.UserHostAddress
. However if you are behind a load balancer or content distribution network then you might want to try looking in Request.Headers["X-Forwarded-For"]
first - this is where the users original IP address will often be written when the request is forwarded on by something.
The approach I'll usually take is to try something along the lines of:
var address = Request.Headers["X-Forwarded-For"];
if (String.IsNullOrEmpty(address))
address = Request.UserHostAddress;
The last project I worked on, we actually logged both, in case the forwarded for header had been faked.
You cannot get the MAC address from the request, however, you can get the IP with Request.UserHostAddress
You cannot get the MAC address of the end-user's machine.
You can get the user's public IP address using Request.UserHostAddress
.
Note the IP address this will not be unique per-user.
If multiple users are behind the same proxy or are on a corporate network, they will usually share the same address.
You can check the X-Forwarded-For
header to get a little more information.
Note that this header can be chained or faked.
public string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = "arp";
pProcess.StartInfo.Arguments = "-a " + ipAddress;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string strOutput = pProcess.StandardOutput.ReadToEnd();
string[] substrings = strOutput.Split('-');
if (substrings.Length >= 8)
{
macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2)) + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6] + "-" + substrings[7] + "-" +
substrings[8].Substring(0, 2);
return macAddress;
}
else
{
return "not found";
}
}
精彩评论