Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Thu, 31-Jul-2014 Authors
Naga116
20
All Time Authors
Jayakumars
4120
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)

How to get MAC Address of client Machine

Bhavna81
Posted by Bhavna81 under Regular Expressions on 6/3/2010 8:10:54 AM | Views : 53283 | Status : [Member] | Replies : 7


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



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

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

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

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


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: Sankarmj@live.com on: 11/25/2011 [Member] Starter | Points: 25

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

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: Sankarmj@live.com on: 11/25/2011 [Member] Starter | Points: 25

@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

Login to post response


Found interesting? Add this to: