2

I am making a WCF application, and I just want to have some extra info on the user connected to server. It doesn't have to be a Unique ID.

I really want something simple. Would this work ?

static UserInfo() // d-constructor
        {

            MachineID = Environment.MachineName;
            MachineID = MachineID + Convert.ToString(Environment.OSVersion);
            MachineID = MachineID + Environment.ProcessorCount;
            MachineID = MachineID + Environment.UserName;
            MachineID = MachineID + Environment.Version;
        }
Kara
  • 6,115
  • 16
  • 50
  • 57
xperator
  • 2,743
  • 7
  • 34
  • 58

1 Answers1

3

Sure, that would work. I checked in in LINQPad 4 quickly, and it came back fine:

MYMACHINEMicrosoft Windows NT 6.1.7601 Service Pack 14myuser4.0.30319.296

You might consider replacing spaces with underscores or something like that if you want the string to be free of spaces - maybe...

MachineID = MachineID.Replace(' ', '_');

...added to the end of your UserInfo() method.

UPDATE:

To follow comments below, Windows 7 Help Forums has quick steps to make a shortcut to run a command as a specific user: to test I setup a shortcut to run cmd.exe as non-admin user myuser, which I created for the purpose. Again, my test here was to then run set and systeminfo from the command line as myuser - testing by analogy but I think sufficient.

UPDATE:

To follow further, I created another shortcut to run LINQPad 4 as non-admin user myuser, then re-run the code: no problemo tested this way too. I aim to please. :)

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 1
    So it doesn't need for any admin privilege? I think only the MachineName and Processorcount has a higher rate of being unique, right? Is there any other Class that I can get more info? – xperator Apr 27 '13 at 08:54
  • I just double checked the possibility of needing admin permissions: nope, all good as a non-admin user. This makes sense because in command-line terms, you are really just getting info that `set` and `systeminfo` provide. Now maybe a policy (e.g. in an AD domain) could snag running as a non-admin; but running as a non-admin user doesn't itself present a problem getting the environment info you specified. – J0e3gan Apr 27 '13 at 09:00
  • 1
    I don't know that `ProcessorCount` is going to add much, unless you are handling requests from machines that may have name collisions (e.g. > 1 machine being named MYMACHINE). IP address may be worth considering, but it really depends on your use-case. `MachineName` and `UserName` should really get most of the way to what you want (i.e. "(No need to be Unique + Doesn't require Admin)"). – J0e3gan Apr 27 '13 at 09:08
  • 1
    Thank you so much. and +1 on that Replace thing. As a beginner, I had no idea you could replace a character in 1 single line. I was thinking about hashing that string, that's why i ignored the spaces. btw, Do you know any other info which can be gathered as well? mac address, motherboard id,or... – xperator Apr 27 '13 at 09:13
  • 1
    Not a problem. Hashing crossed my mind as well, but I wanted to stay on the critical path outlined in your question. :) For additional/different things that you think you might like to include in your machine-user ID, quick searches like "C# MAC address", "C# " at SO, Bing, or Google should lead you quickly to code you can absorb into yours. Bing actually listed [an SO post](http://stackoverflow.com/q/850650/1810429) regarding MAC address that would help with that. – J0e3gan Apr 27 '13 at 09:30