/// <summary>
        /// Gets the username of the currently logged on user after stripping the domain.
        /// </summary>
        /// <returns>The currently logged on user after stripping the domain.</returns>
        /// <remarks>
        /// This will iterate over all sessions to find the first (non-empty) user that
        /// is either being logged in or already logged in. 
        /// </remarks>
        public static string GetUsername()
        {
            // The detected user can be overwritten
            string username = Config.GetUsername();
            if (!string.IsNullOrEmpty(username))
            {
                return username;
            }

            ITerminalServicesManager manager = new TerminalServicesManager();
            using (ITerminalServer server = manager.GetLocalServer())
            {
                server.Open();
                foreach (ITerminalServicesSession session in server.GetSessions())
                {
                    if (!string.IsNullOrEmpty(session.UserName) && (session.ConnectionState == ConnectionState.Initializing || session.ConnectionState == ConnectionState.Active))
                    {
                        return session.UserName;
                    }
                }
            }

            return string.Empty;
        }
Пример #2
0
 public List<clsRDPModel> getSessionListOnLocalhost(string[] accountExceptions=null)
 {
     List<clsRDPModel> result = new List<clsRDPModel>();
     ITerminalServicesManager manager = new TerminalServicesManager();
     using (ITerminalServer server = manager.GetLocalServer())
     {
         IList<ITerminalServicesSession> sessions;
         sessions = server.GetSessions();
         server.Open();
         foreach (ITerminalServicesSession session in sessions)
         {
             NTAccount account = session.UserAccount;
             if (account != null)
             {
                 if(!getAccountIsException(accountExceptions, account.ToString()))
                 {
                     result.Add(new clsRDPModel(
                         session.ConnectionState.ToString(),
                         session.DomainName,
                         session.UserName,
                         (session.ClientIPAddress!=null? session.ClientIPAddress.ToString():""),
                         session.ConnectTime,
                         (session.ConnectionState==ConnectionState.Active? null : session.DisconnectTime),
                         session.SessionId));
                 }
             }
         }
     }
     return result;
 }