public IEnumerable<OnlineUser> List() { try { string ClientPrefix = GetClientPrefix(); ITerminalServicesManager manager = new TerminalServicesManager(); List<OnlineUser> sessions = new List<OnlineUser>(); foreach (string ServerName in ConfigurationManager.AppSettings["RDPServers"].Split(',')) { ITerminalServer server = manager.GetRemoteServer(ServerName); server.Open(); foreach (ITerminalServicesSession session in server.GetSessions()) { if (session.UserName != "" && session.UserName.Substring(0, 3) == ClientPrefix) { OnlineUser onlineUser = new OnlineUser(); onlineUser.SessionId = session.SessionId; onlineUser.UserName = session.UserName; onlineUser.ServerName = session.Server.ServerName; onlineUser.ClientName = session.ClientName; onlineUser.ConnectionState = session.ConnectionState.ToString(); sessions.Add(onlineUser); } } server.Close(); } sessions.Sort((x, y) => string.Compare(x.UserName, y.UserName)); return sessions; } catch (Exception Error) { throw Error; } }
public OnlineUser Find(int SessionId, string ServerName) { ITerminalServicesManager Manager = new TerminalServicesManager(); ITerminalServer Server = Manager.GetRemoteServer(ServerName); Server.Open(); ITerminalServicesSession Session = Server.GetSession(SessionId); OnlineUser onlineUser = new OnlineUser(); onlineUser.SessionId = Session.SessionId; onlineUser.UserName = Session.UserName; onlineUser.ServerName = Session.Server.ServerName; onlineUser.ClientName = Session.ClientName; onlineUser.ConnectionState = Session.ConnectionState.ToString(); return onlineUser; }
public void Logoff(OnlineUser onlineUser) { try { if (onlineUser.UserName.Substring(0,3) != GetClientPrefix()) { throw new Exception("You do not have permission to Logoff session " + onlineUser.SessionId); } ITerminalServicesManager Manager = new TerminalServicesManager(); ITerminalServer Server = Manager.GetRemoteServer(onlineUser.ServerName); Server.Open(); ITerminalServicesSession Session = Server.GetSession(onlineUser.SessionId); Session.Logoff(); } catch (Exception Error) { throw Error; } }