Home All Groups Group Topic Archive Search About

Get current logged on user

Author
28 Feb 2006 10:39 PM
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

Author
28 Feb 2006 11:14 PM
Greg Young
I have dealt with a similar situation before ... I never got the actual
identity of the user but using
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/TermServ/wtsgetactiveconsolesessionid.asp
and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/TermServ/wtsquerysessioninformation.asp

I was able to get the name of the user (including fast switching for
multiple users).

Cheers,

Greg

Show quote
"Niclas" wrote:

> 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
>
>
>
Author
28 Feb 2006 11:33 PM
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?


Show quote
"Greg Young" <GregYo***@discussions.microsoft.com> wrote in message
news:68C833D0-A1A1-4F87-AD86-820C6A762629@microsoft.com...
>I have dealt with a similar situation before ... I never got the actual
> identity of the user but using
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/TermServ/wtsgetactiveconsolesessionid.asp
> and
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/TermServ/wtsquerysessioninformation.asp
>
> I was able to get the name of the user (including fast switching for
> multiple users).
>
> Cheers,
>
> Greg
>
> "Niclas" wrote:
>
>> 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
>>
>>
>>
Author
1 Mar 2006 2:44 AM
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);

    }

}

AddThis Social Bookmark Button