How to get MAC Address of client Machine

Posted by Bhavna81 under Regular Expressions on 6/3/2010 | Views : 71684 | Status : [Member] | Replies : 8
hello experts,
i m developing ASP.NET application. in that for the security purpose i want to use MAC address of the PC. for that i have wrote some code as:
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
for (int j = 0; j <= 1; j++)
{
PhysicalAddress address = nics[j].GetPhysicalAddress();
byte[] bytes = address.GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
M = M + bytes[i].ToString("X2");
if (i != bytes.Length - 1)
{
M = M + ("-");
}
}
}

its working fine. i have host this application on local server in our office. NOW the problem is when user register himself from client machine, above code get the MAC address of server machine not of client machine.

Please Help....
thanks.

My problem is here.......that i want a client Machine's MAC Address when user register himself from client machine.......how can i




Responses

Posted by: Vuyiswamb on: 6/3/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
The Problem here is that you are getting the IP locally on this line

 PhysicalAddress address = nics[j].GetPhysicalAddress();



use this to get the IP of a client

Request.UserHostAddress


Thank you for posting at Dotnetfunda
[Administrator]

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deeraj on: 6/4/2010 [Member] Starter

Up
0
Down
Try this in case you have a load balancer:

public static string GetClientIPAddress(System.Web.HttpRequest httpRequest)
{
string OriginalIP = string.Empty;
string RemoteIP = string.Empty;

OriginalIP = httpRequest.ServerVariables["HTTP_X_FORWARDED_FOR"]; //original IP will be updated by Proxy/Load Balancer.

RemoteIP = httpRequest.ServerVariables["REMOTE_ADDR"]; //Proxy/Load Balancer IP or original IP if no proxy was used

if (OriginalIP != null && OriginalIP.Trim().Length > 0)
{
return OriginalIP + "(" + RemoteIP + ")"; //Lets return both the IPs.
}

return RemoteIP;
}


Hope that helps!

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhavna81 on: 6/4/2010 [Member] Starter

Up
0
Down
public static string GetClientIPAddress(System.Web.HttpRequest httpRequest)

{

string OriginalIP = string.Empty;

string RemoteIP = string.Empty;



OriginalIP = httpRequest.ServerVariables["HTTP_X_FORWARDED_FOR"]; //original IP will be updated by Proxy/Load Balancer.



RemoteIP = httpRequest.ServerVariables["REMOTE_ADDR"]; //Proxy/Load Balancer IP or original IP if no proxy was used



if (OriginalIP != null && OriginalIP.Trim().Length > 0)

{

return OriginalIP + "(" + RemoteIP + ")"; //Lets return both the IPs.

}



return RemoteIP;

}



How to call above function...



Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deeraj on: 6/4/2010 [Member] Starter

Up
0
Down

For Example:

In page_load()
{
string ClientIP = UtilityLibrary.Utilities.Network.GetClientIPAddress(HttpContext.Current.Request);
}

Where UtilityLibrary = Class Library
Utilities = Namespace
Network = Class Name


Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: sankarmjlive-12768 on: 11/25/2011 [Member] Starter | Points: 25

Up
0
Down
UtilityLibrary = Class Library
Could send me the UtiUtilityLibrary dll and tell me the namespace?
its very urgent reply me ....
i'm facing the same problem.
Thanks Bhavna81..thanks for posting

Shankar.M
Think more... Code less

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 11/25/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
look at this

http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.aspx

http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx

Thank you for posting at Dotnetfunda
[Administrator]

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: sankarmjlive-12768 on: 11/25/2011 [Member] Starter | Points: 25

Up
0
Down
@Vuyiswamb you mistaken i have added it....when i compile it shows me that UtilityLibrary doesn't exist in current context
what i have do further

Shankar.M
Think more... Code less

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arunmohan on: 8/25/2014 [Member] Starter | Points: 25

Up
0
Down
The short answer is: No

It is not possible to get the MAC address from the client using JavaScript as this will pose a serious security risk.

The only unique id you will be able to get is the IP address, but this will possibly not be unique as the user's machine might be NATed behind a router.

You could use an ActiveX control on Java applet to get the client machine's MAC address, but this would require installation of the chosen method on the client machine and will never be a fail-safe way of obtaining the MAC address

Bhavna81, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response