Windows Service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

In my service I need to determine the user that is Logged on.
I'm using

WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent())
string name=wp.Identity.Name

but it returns NT ACCOUNT\SYSTEM instead of the actual user name.

I know why its doing this just don't know how to stop it from doing this

does anyone know a way to get the username.
 
Rodney,

The reason it is doing this is because the service runs under a user
account that is configured for the service. In this case, the value being
returned from WindowsIdentity.GetCurrent is correct. You can't guarantee
that there at any point there is only one person logged into the machine, or
any, for that matter.

In order to get around this, you have two choices. The first is to have
this be an application that starts up when the user logs in. In this case,
it would seem appropriate, because your operations are tied into a user
being logged in, not to the machine being on (which is what a service is
more suited for).

The second option is to have your server take requests from an app that
is run when the user logs on. The app would send messages to your service,
along with the identity of the person running the app.

Hope this helps.
 
Back
Top