Пример #1
0
        private void LogoffDisconnectedSessions()
        {
            IntPtr buffer = IntPtr.Zero;
            int    count  = 0;

            if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1,
                                     ref buffer, ref count))
            {
                WTS_SESSION_INFO sessionInfo = new WTS_SESSION_INFO();

                for (int index = 0; index < count; index++)
                {
                    sessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure(
                        new IntPtr(buffer.ToInt32() +
                                   (Marshal.SizeOf(sessionInfo) * index)),
                        typeof(WTS_SESSION_INFO));
                    WTS_CONNECTSTATE_CLASS state = sessionInfo.State;
                    if (state == WTS_CONNECTSTATE_CLASS.WTSDisconnected)
                    {
                        WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE,
                                         sessionInfo.SessionId, true);
                    }
                }
            }
            WTSFreeMemory(buffer);
        }
Пример #2
0
 /// <summary>
 ///     构造函数
 /// </summary>
 /// <param name="userName">用户名称</param>
 /// <param name="domain">Domain</param>
 /// <param name="connectionState">状态</param>
 /// <param name="sessionId">SessionId</param>
 public SessionInfo(string userName, string domain, WTS_CONNECTSTATE_CLASS connectionState, int sessionId)
 {
     UserName        = userName;
     Domain          = domain;
     ConnectionState = connectionState;
     SessionId       = sessionId;
 }
Пример #3
0
 public void UpdateItem(int i, string u, string d, string s, WTS_CONNECTSTATE_CLASS c)
 {
     id           = i;
     username     = u;
     domainname   = d;
     stationname  = s;
     connectstate = c;
 }
Пример #4
0
        private static string GetConnectionState(WTS_CONNECTSTATE_CLASS State)
        {
            string RetVal;

            switch (State)
            {
            case WTS_CONNECTSTATE_CLASS.WTSActive:
                RetVal = "Active";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSConnected:
                RetVal = "Connected";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSConnectQuery:
                RetVal = "Query";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSDisconnected:
                RetVal = "Disconnected";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSDown:
                RetVal = "Down";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSIdle:
                RetVal = "Idle";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSInit:
                RetVal = "Initializing.";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSListen:
                RetVal = "Listen";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSReset:
                RetVal = "reset";
                break;

            case WTS_CONNECTSTATE_CLASS.WTSShadow:
                RetVal = "Shadowing";
                break;

            default:
                RetVal = "Unknown connect state";
                break;
            }

            return(RetVal);
        }
Пример #5
0
        private uint GetUserSession(string partofusername, WTS_CONNECTSTATE_CLASS sessionstate)
        {         // get the first found user session with desired name port or - if name part string is null - with desired sessionstate
            uint   sessionId    = 0xFFFFFFFF;
            IntPtr pSessionInfo = IntPtr.Zero;
            int    sessionCount = 0;
            int    dataSize     = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

            if (WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref pSessionInfo, ref sessionCount) != 0) // IntPtr.Zero = WTS_CURRENT_SERVER_HANDLE
            {                                                                                     // get information array of logon sessions
                IntPtr currentSessionInfo = pSessionInfo;

                for (int i = 0; i < sessionCount; i++)
                {                       // enumerate sessions (walk through array)
                    WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure(currentSessionInfo, typeof(WTS_SESSION_INFO));
                    currentSessionInfo += dataSize;

                    if (partofusername != null)
                    {                     // search for session with part of username
                        IntPtr nameBuffer;
                        int    nameLen;

                        // query for user name of session
                        if (WTSQuerySessionInformation(IntPtr.Zero, si.SessionID, WTS_INFO_CLASS.WTSUserName, out nameBuffer, out nameLen))
                        {                         // Session 0 and "listening" session 65536 return username "\0"
                            string username = Marshal.PtrToStringAnsi(nameBuffer);
                            if (username.Length > 0)
                            {                             // session has a user
                                if (username.ToLower().Contains(partofusername.ToLower()))
                                {                         // user name of session contains search word
                                    sessionId = si.SessionID;
                                    break;
                                }
                            }
                            // free memory
                            WTSFreeMemory(nameBuffer);
                        }
                    }
                    else
                    {                     // check if session has desired state and is not the "service" session 0
                        if ((si.State == sessionstate) && (si.SessionID != 0))
                        {
                            sessionId = si.SessionID;
                            break;
                        }
                    }
                }
                // free memory
                WTSFreeMemory(pSessionInfo);
            }

            return(sessionId);            // retrun found session id or 0xFFFFFFFF
        }
Пример #6
0
 public void UpdateItem(WTSINFO si)
 {
     username       = si.UserName;
     domainname     = si.Domain;
     stationname    = si.WinStationName;
     connectstate   = si.State;
     ConnectTime    = new DateTime(si.ConnectTime).AddYears(1600);
     DisconnectTime = new DateTime(si.DisconnectTime).AddYears(1600);
     LastInputTime  = new DateTime(si.LastInputTime).AddYears(1600);
     LogonTime      = new DateTime(si.LogonTime).AddYears(1600);
     CurrentTime    = new DateTime(si.CurrentTime).AddYears(1600);
     IdleTime       = CurrentTime - LastInputTime;
     SessionTime    = CurrentTime - LogonTime;
 }
Пример #7
0
 public RDPSessionsDTO(uint sessionId, string sessionName, string userName, string domainName, WTS_CONNECTSTATE_CLASS state, string hostName, string farmName, long?lastInputTime, IPAddress?clientIp, string?clientHostname, WTS_CLIENT_DISPLAY?clientResolution, int?clientBuild, byte[]?clientHardwareId, string?clientDirectory)
 {
     SessionID        = sessionId;
     SessionName      = sessionName;
     UserName         = userName;
     DomainName       = domainName;
     State            = state;
     HostName         = hostName;
     FarmName         = farmName;
     LastInputTime    = lastInputTime;
     ClientIp         = clientIp;
     ClientHostname   = clientHostname;
     ClientResolution = clientResolution;
     ClientBuild      = clientBuild;
     ClientHardwareId = clientHardwareId;
     ClientDirectory  = clientDirectory;
 }
Пример #8
0
        public SessionItem(WTSINFO si)
        {
            id             = si.SessionId;
            username       = si.UserName;
            domainname     = si.Domain;
            stationname    = si.WinStationName;
            connectstate   = si.State;
            ConnectTime    = new DateTime(si.ConnectTime).AddYears(1600);
            DisconnectTime = new DateTime(si.DisconnectTime).AddYears(1600);
            LastInputTime  = new DateTime(si.LastInputTime).AddYears(1600);
            LogonTime      = new DateTime(si.LogonTime).AddYears(1600);
            CurrentTime    = new DateTime(si.CurrentTime).AddYears(1600);
            IdleTime       = CurrentTime - LastInputTime;
            SessionTime    = CurrentTime - LogonTime;

            ld = new LoginData(this);
            ld.ProcessLogon();
        }
Пример #9
0
        public static string GetConnectStateString(WTS_CONNECTSTATE_CLASS state)
        {
            switch (state)
            {
            case WTS_CONNECTSTATE_CLASS.WTSActive:
                return("Active");

            case WTS_CONNECTSTATE_CLASS.WTSConnected:
                return("Connected");

            case WTS_CONNECTSTATE_CLASS.WTSConnectQuery:
                return("Connect Query");

            case WTS_CONNECTSTATE_CLASS.WTSDisconnected:
                return("Disconnected");

            case WTS_CONNECTSTATE_CLASS.WTSDown:
                return("Down");

            case WTS_CONNECTSTATE_CLASS.WTSIdle:
                return("Idle");

            case WTS_CONNECTSTATE_CLASS.WTSInit:
                return("Init");

            case WTS_CONNECTSTATE_CLASS.WTSListen:
                return("Listen");

            case WTS_CONNECTSTATE_CLASS.WTSReset:
                return("Reset");

            case WTS_CONNECTSTATE_CLASS.WTSShadow:
                return("Shadow");
            }
            return(""); // This should never happen.
        }
Пример #10
0
        public static List<SessionInfo> ListSessions(string serverName, string userName, string domainName, 
            string clientName, WTS_CONNECTSTATE_CLASS? state)
        {
            IntPtr server = IntPtr.Zero;
            List<SessionInfo> sessions = new List<SessionInfo>();
            server = OpenServer(serverName);
            try
            {
                IntPtr ppSessionInfo = IntPtr.Zero;
                Int32 count = 0;
                Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
                Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
                Int32 current = (int)ppSessionInfo;
                if (retval != 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        SessionInfo sessionInfo = new SessionInfo();
                        WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
                        current += dataSize;

                        sessionInfo.Id = si.SessionID;
                        sessionInfo.UserName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSUserName);
                        sessionInfo.DomainName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSDomainName);
                        sessionInfo.ClientName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSClientName);
                        sessionInfo.State = si.State;

                        if (userName != null || domainName!=null || clientName != null || state!=null) //In this case, the caller is asking to return only matching sessions
                        {
                            if (userName != null && !String.Equals(userName, sessionInfo.UserName, StringComparison.CurrentCultureIgnoreCase))
                                continue; //Not matching
                            if (clientName != null && !String.Equals(clientName, sessionInfo.ClientName, StringComparison.CurrentCultureIgnoreCase))
                                continue; //Not matching
                            if (domainName != null && !String.Equals(domainName, sessionInfo.DomainName, StringComparison.CurrentCultureIgnoreCase))
                                continue; //Not matching
                            if (state != null && sessionInfo.State != state.Value)
                                continue;
                        }

                        sessions.Add(sessionInfo);
                    }
                    WTSFreeMemory(ppSessionInfo);
                }
            }
            finally
            {
                CloseServer(server);
            }
            return sessions;
        }
Пример #11
0
        private static string GetConnectionState(WTS_CONNECTSTATE_CLASS State)
        {
            string RetVal;

            switch (State)
            {
                case WTS_CONNECTSTATE_CLASS.WTSActive:
                    RetVal = "Active";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSConnected:
                    RetVal = "Connected";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSConnectQuery:
                    RetVal = "Query";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSDisconnected:
                    RetVal = "Disconnected";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSDown:
                    RetVal = "Down";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSIdle:
                    RetVal = "Idle";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSInit:
                    RetVal = "Initializing.";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSListen:
                    RetVal = "Listen";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSReset:
                    RetVal = "reset";
                    break;
                case WTS_CONNECTSTATE_CLASS.WTSShadow:
                    RetVal = "Shadowing";
                    break;
                default:
                    RetVal = "Unknown connect state";
                    break;
            }

            return RetVal;
        }