Get current logged on user

N

Niclas

Is it possible to configure a Windows Service to:

1. Start the service when a user logs on (i.e. service not active before
logon)

2. Determine the WindowsIdentity of the currently logged on user

Niclas
 
G

Gabriel Magaña

Did you take into account remote desktop and/or terminal services? This
would mean that several users have valid session concurrently... How did
you deal with that screnario?
 
W

William Stacey [MVP]

| 1. Start the service when a user logs on (i.e. service not active before
| logon)

You could use the ServiceControl control to start service when your app
starts. Or put your app in the Startup folder.

| 2. Determine the WindowsIdentity of the currently logged on user

private void button18_Click(object sender, EventArgs e)

{

WindowsIdentity wi = WindowsIdentity.GetCurrent();

Console.WriteLine("Current User: " + wi.Name);

foreach (IdentityReference ir in wi.Groups)

{

NTAccount acc = (NTAccount)ir.Translate(typeof(NTAccount));

Console.WriteLine("Member of: " + acc.Value);

}

}
 

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