Reputation: 99
I have a basic setup to deal with here, no domains or fancy stuff. I can make a program receive a message from my phone to trigger events, etc.
How can a C# application run in the background before login? (I'll try as a service first...)
And, how can that program trigger a logon into Windows? It would be a personal app, so I'm not concerned too much with security.
some pseudo-code (Yes, I know it's not secure):
LogOnWindows(string WindowsUserName, string WindowsPassword)
{
if (Environment.GetEnvironmentVariable(USERNAME) == null)
{
LogonWindows(WindowsUserName, WindowsPassword);
}
}
I've checked out the LogOnUser API call for Windows but that seems to log the user on in the background...
Upvotes: 1
Views: 1772
Reputation: 15769
There are many, many good security reasons why this is very, very hard for an application to do. As Raymond Chen would say, "imagine if any application could do this".
The proper way to do this (which is probably FAR more trouble than you are going to want to deal with) is to create a GINA replacement. You can look here for an example.
But again, stop, think and consider the implication of the simple statement of "I want an application to be able to log on as anyone without user interaction". This was possible in Windows 9x, and it was a feature removed by design. Think about that.
Upvotes: 1
Reputation: 18965
The only way I've seen this done in the past is to implement something similar to VNC where you control mouse and keyboard input from a service. A good starting point would be to review the UltraVNC source code or something similar. (Warning it is written in C++ and not for the faint of heart).
EDIT: If you're comfortable with unmanaged code (C++ or similar) this could be be easier implemented using one of the suggestions posted here.
Upvotes: 0
Reputation: 44931
If the service is always to be run as a given user (i.e. it doesn't have to change users in response to requests), then you would just set the specified user's credentials in the service's Log On tab and not worry about programmatically changing the credentials.
Otherwise, you need to use the LogonUser API and pass the token from that API to a new WindowsIdentity instance and then use the Impersonate method on that new identity. There is a good article on what is involved here.
Also forgot to mention: if you are running as a service the your method to use Environment.GetEnvironmentVariable will not get the service user's information, not the interactive user information (technically, depending on the OS, there could be multiple simultaneous interactive users).
Upvotes: 0