简体   繁体   中英

C# Logoff out a user via Windows Service

How do I logoff a user via a Windows Service?

I found this example here but it will not work in a service. http://www.c-sharpcorner.com/UploadFile/thiagu304/desktopfunctions02112007140806PM/desktopfunctions.aspx

Check out WTSLogoffSession. It does precisely what you want and is pretty easy to call via PInvoke. To get the session ID use WTSGetActiveConsoleSessionId or WTSEnumerateSessions and WTSQuerySessionInformation.

HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server --->>>> Set DWORD32 AllowRemoteRPC == 1

The Remote Procedure Call (RPC) service [RpcSs] should be running on a computer by default.

We need this enabled to use the following commands:

  • To get sessions == [qwinsta /SERVER:"XXXXXX"]
  • To end active session, use session name of "console" == [logoff "console" /SERVER:"XXXXXX"]
  • To end particular session where N is session ID == [logoff "N" /SERVER:"XXXXXX"]

I believe those commands use the functions you are trying to use behind the scenes, therefore if those commands succeed then that means the functions will succeed.

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