Getting the currently logged in user

S

simonlpwaters

I need to obtain the username of the currently logged in user on a
machine. However, the program that needs to do this will be running
under different credentials to the logged in user, so using
Environment.UserName will give the user name of the user that is
running the program.

Any help would be appreciated, this is giving me a headache.
 
P

PS

I need to obtain the username of the currently logged in user on a
machine. However, the program that needs to do this will be running
under different credentials to the logged in user,

What if there is more than one logged in user?

PS

so using
 
A

andy

What if there is more than one logged in user?

PS

so using





- Show quoted text -

So this is otherwise a straight windows app?
Maybe you just want to use the different credentials for tasks the
other credentials are necessary for.
Like make your database connection always using a specific user and
password.
Don't ask me what you do about file access authorities etc though.
 
N

Nicholas Paldino [.NET/C# MVP]

Are you running the program using the "Run As" item when you right click
on the executable? If so, then you really won't be able to determine who
the user who launched the program is.

If you are impersonating the user credentials yourself in your program,
then all you have to do is make sure you get the credentials before you
begin to impersonate.


Hope this helps.
 
J

John Timney \(MVP\)

As far as your program is concerned, that is the correct user name as the
thread is runs under is the current user. Can you get the username before
or as you start your program?

You could perhaps try to PInvoke on WNetGetUser give you the user who logged
into the network - which is not the same as UserName but may not be
appropriate if you have more than one logged in user.
http://vbnet.mvps.org/index.html?code/network/netuserenum.htm

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 
W

Willy Denoyette [MVP]

I need to obtain the username of the currently logged in user on a
machine. However, the program that needs to do this will be running
under different credentials to the logged in user, so using
Environment.UserName will give the user name of the user that is
running the program.

Any help would be appreciated, this is giving me a headache.


Using System.Management you can get at the parent user ID, of course this requires that your
process is spawned from the interactive logon session, for instance the explorer shell or
the command line shell.
Following method gets the current process -> parent process -> associated logon session ->
LogonUser.

static string GetParentUser(int pid)
{
string parentUserAccount = null;
string queryString = String.Format("select ParentProcessId from win32_process where
ProcessId={0}", pid);
using(ManagementObjectSearcher query = new ManagementObjectSearcher(new
SelectQuery(queryString))) {
foreach( ManagementObject mo in query.Get()) {
uint parentPid = (uint)mo.Properties["ParentProcessId"].Value;
queryString = String.Format("select Handle from win32_process where ParentProcessId =
{0}", parentPid);
using(ManagementObjectSearcher subQuery = new ManagementObjectSearcher(new
SelectQuery(queryString))) {
foreach( ManagementObject mo1 in subQuery.Get()) {
string handle = (string)mo1.Properties["Handle"].Value;
RelatedObjectQuery relatedQuery =
new RelatedObjectQuery ("associators of {Win32_Process.Handle=\"" + handle + "\"}");
relatedQuery.RelatedClass = "Win32_LogonSession";
using(ManagementObjectSearcher relQuery = new ManagementObjectSearcher(relatedQuery))
{
foreach( ManagementObject mo2 in relQuery.Get()) {
RelatedObjectQuery relQuery2 =
new RelatedObjectQuery ("associators of {Win32_LogonSession.LogonId='" +
mo2["LogonId"]+ "'}");
relQuery2.RelationshipClass = "win32_LoggedonUser";
using(ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(relQuery2))
{
foreach (ManagementObject mo3 in searcher2.Get()) {
parentUserAccount = String.Format(@"{0}\{1}", mo3["Domain"], mo3["Name"]);
}
}
}
}
}
}
}
}
return parentUserAccount;
}

// usage...
..
Process proc = System.Diagnostics.Process.GetCurrentProcess();
string parentId = GetParentUser(proc.Id));
..

Note that you can call this at any time in a spawned process, however, if you are
impersonating, you have to call this method (GetParentUser) before impersonating.
Note that Vista uses plit tokens when running non elevated with UAC enabled, so you'll get
two times the same account back, this is dealt with by :
parentUserAccount += String.Format(@"[{0}\{1}]", mo3["Domain"], mo3["Name"]);

Willy.
 
D

D. Yates

Willy,

I know that this will throw a SecurityException if you are impersonating,
but will this work for the situation at hand:

WindowsIdentity wi = WindowsIdentity.GetCurrent();

wi.Name


Dave
 
W

Willy Denoyette [MVP]

D. Yates said:
Willy,

I know that this will throw a SecurityException if you are impersonating, but will this
work for the situation at hand:

WindowsIdentity wi = WindowsIdentity.GetCurrent();

wi.Name


Dave

Dave,
Above will return the current process identity or the impersonating identity (it will not
throw an exception), however, it will not return the identity of the logon session when the
process runs in different account.
Here is what I mean:
1. Logon as BOB
2. Start another process using "Run As" , or, start a process using "Process.Start" as ALICE
3. above will return ALICE, the code I posted will return BOB.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top