How do I programmatically pull / access the computer name on any given machine (laptop / desktop) etc.?

Michael Sterling
Michael Sterling used Ask the Experts™
on
How do I programmatically pull / access the computer name on any given machine (laptop / desktop) etc.?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Are you referring to client machines that access your website? If so, is this an Intranet or Internet site?
To get the machine name of the computer running the program:
  In C# you can use:
   name = System.Environment.MachineName

  In classic ASP (VBScript) you can use:
   Set objComputer = CreateObject("Shell.LocalMachine")
   name = objComputer.MachineName

   
To expand on kaufmed's comment, if you are trying to get the computer name of client machines that access your website, it will only work for machines on your local network, as machine names are concept of local networks. If any of them connect via the internet to your server they won't give a machine name, just IP address.

To get client computer names of machines on your local network, you will need to use reverse DNS:

In C#:
string clientMachineName;
        clientMachineName = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName);
        Response.Write(clientMachineName);

In classic ASP you can use something like this: http://www.asp101.com/articles/jason/reversedns/default.asp
Michael SterlingWeb Applications Developer

Author

Commented:
@kaufmed & @matthewstevenkelly: well i guess i'll need to get the IP address then. what i'm attempting to do is limit where (which machines) users can do certain things within my web site that will be eventually hosted on goDaddy. I only want certain functionality available if the user is on machine(s) x, y, z...is there a better way of doin this? how would this "plan" be affected if the user were to use a laptop?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
I'm not going to be the best one to answer this, but my initial thought is that you may have problems trying to limit people by IP address. Most users are assigned an IP address to their machine/modem via DHCP. Because of this, there is a good chance their IP addresses can change. I would expect that you would want to limit by authentication (e.g. username/password) rather than by machine.
Using the IP address is not a good method for authentication because it can be spoofed: http://en.wikipedia.org/wiki/IP_address_spoofing

Additionally, when multiple users are behind the same router, they will all appear to your website with the same IP address.

You can see this for yourself, if you have a home router, and more than one computer connected to it, go to http://www.whatismyip.com/ and they will both show the same IP address. And as mentioned previously, most users have a dynamic IP address, so it changes every time the router is rebooted.


You can get a users IP address in C# by using: Request.ServerVariables["remote_addr"]

But it should only be used as an FYI, not authentication.


If you don't need a lot of security, you could just use cookies to track the machine. Basically when the user goes to a website a cookie will be put on their computer that your website could then check for to know if they have been there before. But this should only be used if you are trying to remember if the user has been there before, not to get any kind of account information as it is highly insecure.

Otherwise, username and password is how you will need to go.
Michael SterlingWeb Applications Developer

Author

Commented:
thanks for your contributions

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial