简体   繁体   中英

Disable Windows user's interactive login using C# service

Is it possible to use a Windows service (C# - Local System priv.) to deny an interactive login?

What i want to achieve is when a user enters username and password, to be up to my service whether to allow him in or not.

Currently i do this by polling current interactive username every X seconds, and performing logout if needed. But i'd like to avoid this delay time because it allows user to login even for a few seconds. It would be more elegant to just deny logging in from the beginning.

As you're in a Windows Service, you can register yourself to handle the SERVICE_CONTROL_SESSIONCHANGE event. First, you must enable your service to handle this event :

public MyService()
{
    ...
    CanHandleSessionChangeEvent = true;
}

And then just override the OnSessionChange method :

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    if (changeDescription.Reason == SessionChangeReason.SessionLogon)
    {
        // do work
    }
}

If you need more informations to do your work, I let you check the WTS API which will provide you dozens of informations (username is one of them) using the changeDescription.SessionId attribute (for example).

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