简体   繁体   中英

How do I programatically log off the current user and log on as another?

Is there a way to force a logoff (knowing this is not recommended) of the current interactive (logged-on) user and then login with a new user account (interactive - not using stuff like LogonUser and other impersonation tricks) to simulate someone physically pressing LOGOFF and then selecting a new account to LOGON...?

The issue is that my service creates a new user account programatically - and I need to find a way to create its corresponding user profile. From what I can see the only way that works would be to physically logoff the current user, and then login as the new user (which will create the User Profile for HKCU, Documents & Settings, etc...). Sounds a little crazy but I can't find a better way...

So, I need to find a way to programmatically force the logoff & logon actions (from my running Windows Service under LocalSystem) - I have all the credentials and access rights needed ... Are there system function calls I can use?

For Logoff I think I can use WTSLogoffSession(...), still trying to get it to work but seems promising - however I have no clue what to do about logon... Alternitively if there is a better approach to solve my problem I am all ears :)

Any help or hints would be much appreciated... Thanks,

Do you actually need to logon the user or just create one? For creation you can use

  • NetGroupAdd()
  • NetUserAdd()
  • NetGroupAddUser()
  • NetLocalGroupAdd()
  • NetLocalGroupAddMember()

APIs

Microsoft has this solution in c++, which you can use to hack ac# solution, or put in a DLL and call it using COM interop.

http://support.microsoft.com/kb/196070

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM