By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

Getting the currently logged in user

P: n/a
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.

Apr 11 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
PS
<si***********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>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
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.

Apr 11 '07 #2

P: n/a
On 11 Apr, 13:56, "PS" <ecneserpeg...@hotmail.comwrote:
<simonlpwat...@gmail.comwrote in message

news:11*********************@b75g2000hsg.googlegro ups.com...
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
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.- Hide quoted text -

- 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.

Apr 11 '07 #3

P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<si***********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>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.

Apr 11 '07 #4

P: n/a
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?cod...etuserenum.htm

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
<si***********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>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.

Apr 11 '07 #5

P: n/a
<si***********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>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.

Apr 11 '07 #6

P: n/a
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
Apr 11 '07 #7

P: n/a
"D. Yates" <fo****@hotmail.comwrote in message
news:eX**************@TK2MSFTNGP03.phx.gbl...
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.



Apr 12 '07 #8

P: n/a
Willy,

Got it. Your patience is appreciated.

Thanks,
Dave
Apr 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.