473,396 Members | 2,013 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,396 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 28577
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.