473,240 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

Getting the currently logged in user

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
8 28529
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
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
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
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
<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
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
"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
Willy,

Got it. Your patience is appreciated.

Thanks,
Dave
Apr 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcin Zmyslowski | last post by:
Hello all! I want to create a page in ASP language which gives me information about currently logged users. I thought to do it by this way: when somebody will log in to page, there will be a...
1
by: anonymous | last post by:
Hi all, I've been searching the way to achieve the following task. But no luck so far. I have a web site(main site), which requires authentication. This authentication is set at Windows...
6
by: zoltix | last post by:
Hi, I would like to access to execute specific commands but the privileges are not enough from an aspx Page. Because this service run as IU_IISSERVER, therefore aspx hasn’t access to these...
2
by: SStory | last post by:
I have a service which has no U/I, that I use to ensure a certain tray application is running. I would like for the service which runs under "Local System", to create the process as a normal app...
1
by: eswanson | last post by:
How do you get the currently logged in user from a regular c sharp class? I have a common utility class in which I have some static functions that called from multiple places. In one of the...
7
by: John | last post by:
Hi We can get the current logged in user's name but is it also possible to get the default email form outlook of the currently logged-in user as well? The reason for this is that I need to email...
6
by: MuZZy | last post by:
Hi, I am looking to find a way to get currently logged in user's object GUID without querying ActiveDirectory. For example, when i log in to my laptop from home, I'm not on the office network so...
2
by: gihope | last post by:
Hi, can anyone advise me how I can access the UserName of a currently logged in user without using the LoginName control. For instance I want to search tables I have created in my database that I...
2
by: sgawas01 | last post by:
i want the username of currently logged in user on client machine. want to use it for intranet authentication. i dont want my application to show any popup asking for username and password. it...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.