YZlat
asked on
How to retrieve client machine name from javascript?
I have an ASP.NET application that is located on the server but is accessed from client machines. I need the application to retrieve computer name of the client computer that accesses the application. How can I do that using javascript?
P.S. I've already tried Environment.MachineName and Request.ServerVariables{"H OSTNAME"]. All these will only retrieve either IP Address or Server machine name. I need a client machine name
P.S. I've already tried Environment.MachineName and Request.ServerVariables{"H
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe the only way to truly do this is on the server, first you get the IP, and do a DNS lookup to get the computer name. This will only work in an intranet where you have a local DNS server with all the computer names.
ASKER
thanks raterus. and how do I do that? can you post some code?
Here is vbscript solution . http://www.codeproject.com/vbscript/userinfo.asp
I don't have any code to do this off the top of my head, I could google it probably just as easily as you could. ".net DNS lookup"
ASKER
I wrote this function to retrieve computer name, but I still need to get at least the IP Address of the client machine in order to pass it to my function. ANd Request.ServerVariables["R EMOTE_ADDR "] gives me the IP of the localhost and not client computer
public string ComputerName(string ipaddr)
{
System.Net.IPHostEntry objX;
string strHostName="";
try
{
//get the host dns entry
objX = System.Net.Dns.GetHostByAd dress(ipad dr);
//get the hostname property
strHostName = objX.HostName;
}
catch (Exception ex)
{
//something went wrong (possibly no entry)
}//end try
return strHostName;
}
public string ComputerName(string ipaddr)
{
System.Net.IPHostEntry objX;
string strHostName="";
try
{
//get the host dns entry
objX = System.Net.Dns.GetHostByAd
//get the hostname property
strHostName = objX.HostName;
}
catch (Exception ex)
{
//something went wrong (possibly no entry)
}//end try
return strHostName;
}
Try this to get the IP address
string ipaddress;
if (Request.ServerVariables[" HTTP_X_FOR WARDED_FOR "] != "")
{
ipaddress = Request.ServerVariables["H TTP_X_FORW ARDED_FOR" ];
}
else
{
ipaddress = Request.ServerVariables["R EMOTE_ADDR "];
}
string ipaddress;
if (Request.ServerVariables["
{
ipaddress = Request.ServerVariables["H
}
else
{
ipaddress = Request.ServerVariables["R
}
ASKER
raterus, I get ipaddress=null
You've got something weird going on in the configuration of your server. REMOTE_ADDR should work, period, and not be the server's ip.
Wait a minute... when did it go server side ? I thought you wanted to do it client side...???
ASKER
It's really not important if it's done on client side or server-side, as long as I retrieve the name of the client machine, from which the web page is opened
Keeping it simple, I think nisarkhan's first comment answered the question. We were getting into server side scripting which wasn't really the original question.
ASKER
I guess you are right
ASKER
In other words, from a web page opened on computer1 I need to get the name of the computer1. I cannot do that with server-side code, but that's done with client-side code. JavaScript is client-side language