Exemplo n.º 1
0
        private static void UnattendedclientUpdated(unattendedclient unattendedclient)
        {
            if (unattendedclient == null)
            {
                return;
            }
            try
            {
                RobotUserSession session = null;
                if (sessions != null)
                {
                    session = sessions.Where(x => x.client._id == unattendedclient._id).FirstOrDefault();
                }
                if (!unattendedclient.enabled)
                {
                    if (session != null)
                    {
                        session.client = unattendedclient;
                        if (session.rdp != null || session.freerdp != null)
                        {
                            Log.Information("disconnecting session for " + session.client.windowsusername);
                            try
                            {
                                session.disconnectrdp();
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex.ToString());
                            }
                        }
                    }
                    Log.Information("GetOwnerExplorer");
                    System.Diagnostics.Process ownerexplorer = RobotUserSession.GetOwnerExplorer(unattendedclient);
                    if (ownerexplorer != null)
                    {
                        if (server.logoff || session.client.autosignout)
                        {
                            Log.Information("WTSLogoffSession " + ownerexplorer.SessionId);
                            NativeMethods.WTSLogoffSession(IntPtr.Zero, (int)ownerexplorer.SessionId, true);
                        }
                        else
                        {
                            Log.Information("WTSDisconnectSession " + ownerexplorer.SessionId);
                            NativeMethods.WTSDisconnectSession(IntPtr.Zero, (int)ownerexplorer.SessionId, true);
                        }
                    }
                }
                if (unattendedclient.enabled)
                {
                    if (server != null && server.singleuser)
                    {
                        Log.Information("GetOwnerExplorer");
                        System.Diagnostics.Process ownerexplorer = RobotUserSession.GetOwnerExplorer(unattendedclient);
                        int sessionid = -1;
                        if (ownerexplorer != null)
                        {
                            sessionid = ownerexplorer.SessionId;
                        }
                        var procs = System.Diagnostics.Process.GetProcessesByName("explorer");
                        foreach (var explorer in procs)
                        {
                            if (explorer.SessionId != sessionid)
                            {
                                if (server.logoff || session.client.autosignout)
                                {
                                    Log.Information("WTSLogoffSession " + explorer.SessionId);
                                    NativeMethods.WTSLogoffSession(IntPtr.Zero, (int)explorer.SessionId, true);
                                }
                                else
                                {
                                    Log.Information("WTSDisconnectSession " + explorer.SessionId);
                                    NativeMethods.WTSDisconnectSession(IntPtr.Zero, (int)explorer.SessionId, true);
                                }
                            }
                        }
                    }
                    else if (server == null)
                    {
                        Log.Information("server is null!!!");
                    }

                    if (session != null)
                    {
                        Log.Information("Updating session for " + unattendedclient.windowsusername);
                        session.client = unattendedclient;
                        if (session.rdp == null && session.freerdp == null)
                        {
                            session.BeginWork();
                        }
                    }
                    else
                    {
                        Log.Information("Adding session for " + unattendedclient.windowsusername);
                        sessions.Add(new RobotUserSession(unattendedclient));
                    }
                }
                cleanup();
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
            finally
            {
            }
        }