简体   繁体   English

通过组策略锁定计算机时,C#SessionSwitchReason.SessionLock不触发

[英]C# SessionSwitchReason.SessionLock NOT triggering when machine is locked via Group Policy

EDIT: The issue here wan't the fact it was locked via GP, it was that it was being run as a service under a service account and it didn't have access to the interactive desktop 编辑:这里的问题不是事实,它是通过GP锁定的,它是作为服务在服务帐户下运行的,并且它无法访问交互式桌面

I have a C# application that needs to check for when a user's session is locked, I'm using Microsoft.Win32.SystemEvents.SessionSwitch and this works fine when a user manually locks the machine. 我有一个C#应用程序,需要检查用户的会话何时被锁定,我使用的是Microsoft.Win32.SystemEvents.SessionSwitch ,当用户手动锁定计算机时,它可以正常工作。

The problem is that when the machine is locked via a group policy (User Configuration > Policies > Administrative Templates > Personalization > Screen saver timeout) the application doesn't pick up the switch. 问题是,当计算机通过组策略(用户配置>策略>管理模板>个性化>屏幕保护程序超时)锁定时,应用程序将无法启动交换机。

Is there another way to check for a machine being locked? 还有另一种方法检查机器是否被锁定? Or is there another way to lock machines via group policy that will be picked up by the application? 还是有另一种通过组策略锁定计算机的方法,该策略将由应用程序选择?

NB The application is running on windows 7 as a service with full admin rights 注意:该应用程序作为具有完全管理员权限的服务在Windows 7上运行

Here's my code, Thanks in advance!!! 这是我的代码,在此先谢谢!!! :) :)

     public void OnStart(string[] args)
            {

            Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            }


    void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
           {

            if (e.Reason == SessionSwitchReason.SessionLock)
                  {
                   //DO STUFF
                  }
           }

I managed to resolve this by enabling 'Other Logon/Logoff Events' in Windows Event Viewer and searching for the lock and unlock events. 我设法通过在Windows事件查看器中启用“其他登录/注销事件”并搜索锁定和解锁事件来解决此问题。

    //Define strings for searching the eventlog.
    string lockEvent = "4800";
    string unlockEvent = "4801";

     //Define the Eventlog source you want (in this case it's Security)
    string LogSource = @"Security";

    //Add these together to make the full query for Lock and Unlock
    string LockQuery = " *[System/EventID=" + lockEvent + "]";
    string UnlockQuery = "*[System/EventID=" + unlockEvent + "]";


//Return true if there is any locked events found.

    private bool CheckForLock()
    {
        //Create Eventlog Reader and Query
        var elQuery = new EventLogQuery(LogSource, PathType.LogName, LockQuery);
        var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery);

        //Create a list of Eventlog records and add the found records to this
        List<EventRecord> eventList = new List<EventRecord>();
                for (EventRecord eventInstance = elReader.ReadEvent();
                    null != eventInstance; eventInstance = elReader.ReadEvent())
                {

                   eventlist.add(eventInstance);

                 }

          if(eventList.count > 0)
              {
                  return true;
              }
           else
             { 
                 return false;
             }

        }

NB This will check all the event log, so you need to put a qualifier on how far into the past you want to bee looking. 注意:这将检查所有事件日志,因此您需要在限定符上标注您想看的过去。 If you check for lock/unlock sessions every ten seconds, you only want to deal with an EventRecord if it's from the same time period. 如果您每隔十秒钟检查一次锁定/解锁会话,则只想处理来自同一时间段的EventRecord You can access the eventlist.TimeCreated value to do something like... 您可以访问eventlist.TimeCreated值来执行以下操作:

        if (eventInstance.TimeCreated > DateTime.Now - TimeSpan.FromSeconds(10))
        {
            eventList.Add(eventInstance);

        }

Is it elegant? 优雅吗? No. Does it work? 不行吗? Yes. 是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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