Ejemplo n.º 1
0
            public async Task Run()
            {
#if !DEBUG
                if (WorkstationHelper.GetActiveSessionLockState() == WorkstationHelper.LockState.Unlocked)
                {
                    try
                    {
                        SessionSwitchMonitor.SessionSwitch += SessionSwitchMonitor_SessionSwitch;

                        _wcfLocker.LockWorkstation();

                        await Task.WhenAny(_tcs.Task, Task.Delay(_lockTimeout));
                    }
                    finally
                    {
                        SessionSwitchMonitor.SessionSwitch -= SessionSwitchMonitor_SessionSwitch;
                    }

                    if (!_tcs.Task.IsCompleted &&
                        WorkstationHelper.GetActiveSessionLockState() == WorkstationHelper.LockState.Unlocked)
                    {
                        _wtsapiLocker.LockWorkstation();
                    }
                }
#endif
            }
Ejemplo n.º 2
0
        public void LockWorkstation()
        {
            var lockState = WorkstationHelper.GetActiveSessionLockState();

            if (lockState == WorkstationHelper.LockState.Unlocked)
            {
                IntPtr ppSessionInfo  = IntPtr.Zero;
                IntPtr userPtr        = IntPtr.Zero;
                IntPtr domainPtr      = IntPtr.Zero;
                Int32  count          = 0;
                Int32  retval         = WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref ppSessionInfo, ref count);
                Int32  dataSize       = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
                var    currentSession = ppSessionInfo;
                uint   bytes          = 0;

                if (retval == 0)
                {
                    return;
                }

                WriteLine("Query sessions");
                for (int i = 0; i < count; i++)
                {
                    WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO));
                    currentSession += dataSize;

                    WTSQuerySessionInformation(IntPtr.Zero, si.SessionID, WTS_INFO_CLASS.WTSUserName, out userPtr, out bytes);
                    WTSQuerySessionInformation(IntPtr.Zero, si.SessionID, WTS_INFO_CLASS.WTSDomainName, out domainPtr, out bytes);

                    var domain          = Marshal.PtrToStringAnsi(domainPtr);
                    var userName        = Marshal.PtrToStringAnsi(userPtr);
                    var sessionFullName = domain + "\\" + userName;

                    WTSFreeMemory(userPtr);
                    WTSFreeMemory(domainPtr);

                    // Note: it might be a good idea to limit session disconnects only to those activated by triggered device
                    //if (sessionFullName == sessionTolock)
                    if (!string.IsNullOrWhiteSpace(domain) && !string.IsNullOrWhiteSpace(userName))
                    {
                        if (si.State == WTS_CONNECTSTATE_CLASS.WTSActive)
                        {
                            WriteLine($"Disconnecting session: {sessionFullName}");
                            bool disconnected = WTSDisconnectSession(IntPtr.Zero, si.SessionID, true);
                            WriteLine($"Session disconnected: {disconnected}");
                        }
                        else
                        {
                            WriteLine($"Session inactive: {sessionFullName}");
                        }
                    }
                    else
                    {
                        WriteLine($"Session skipped: {sessionFullName}");
                    }
                }
                WTSFreeMemory(ppSessionInfo);
            }
        }
Ejemplo n.º 3
0
 void LockWorkstationAsync()
 {
     Task.Run(async() =>
     {
         var lockState = WorkstationHelper.GetActiveSessionLockState();
         if (lockState == WorkstationHelper.LockState.Unlocked)
         {
             try
             {
                 await _messenger.Publish(new LockWorkstationMessage());
             }
             catch (Exception ex)
             {
                 WriteLine(ex);
             }
         }
     });
 }