Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
My application is hosted in server. application is available for which client who are connected with that server. In this articture i want to get the client machine info like ip,mac,machine name.


What I have tried:

public string GetVisitorIPAddress(bool GetLan = false)
{
string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (String.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

if (string.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = HttpContext.Current.Request.UserHostAddress;

if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
{
GetLan = true;
visitorIPAddress = string.Empty;
}

if (GetLan)
{
if (string.IsNullOrEmpty(visitorIPAddress))
{
//This is for Local(LAN) Connected ID Address
string stringHostName = Dns.GetHostName();
//Get Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;

try
{
visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
}
catch
{
try
{
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
try
{
arrIpAddress = Dns.GetHostAddresses(stringHostName);
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
visitorIPAddress = "127.0.0.1";
}
}
}
}
}
return visitorIPAddress;
}
Posted
Updated 14-Jun-16 1:57am
Comments
ZurdoDev 14-Jun-16 7:52am    
And what is the problem?
F-ES Sitecore 14-Jun-16 9:56am    
Google "asp.net find client ip address", "asp.net find client mac address" as this is asked all the time. I'll give you the Cliffs Notes though; if it is across the internet you can't.

1 solution

Assuming this is web based, you can't get that information at your server.
The IP address of the client PC is meaningless, as it's a local network address (probably 192.168.x.x) and it is only relevant to the local LAN segment on which it's attached. The IP address you can get is that of the router or other point of connection to the internet and is shared by all equipment accessing the internet through it.
The MAC address is not transferred past the router so you can't get that either.
And the Machine Name? You can't get that either, for security reasons.

In fact, you can get all of these under very specialized circumstances, which do not occur in 99.99% of instances: the computer has to be a PC, the browser has to be IE, It has to have ActiveX control downloads permitted, and ActiveX controls have to be allowed to run. Both of the last two are disabled by default.
 
Share this answer
 
Comments
prakash00060 16-Jun-16 7:09am    
Thanks for your comment and support.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900