2016-09-28 66 views
0

就像標題所說,我想創建一個方法來搜索Windows用戶會話ID由域名\用戶名。c#如何確定特定登錄的Windows用戶的會話ID用戶

我已經完成了讀取當前登錄的用戶,但id不知道確定他們的會話ID。

我的目標是殺死特定用戶的會話。

+0

的[註銷的Windows用戶本地使用C#]可能的複製(http://stackoverflow.com/questions/14466373/log-off-a-windows-user-locally-using-c-sharp) – Amy

回答

0

創建一個命令行應用程序。 調用命令「查詢會話」並將結果存儲在例如sessionList的列表中。 比你可以說:

var sessionList = Process.Start("cmd", "c:\>query session").ToList(); 
 
sessionList = sessionList.Where(x => x.Username == "Piet") 
 
foreach(var item in sessionList) 
 
{ 
 
console.WriteLine(item.id) 
 
} 
 

也許你需要微調它一點點,但它將基於從代碼工作

+0

沒有方法.toList()for Process.Start 你能告訴我怎樣才能將輸出發送到列表? – MantiCore

+0

http://www.howtogeek.com/howto/programming/get-a-list-of-running-processes-in-c/ – 2016-09-29 13:26:42

+0

如果我運行進程cmd查詢會話我沒有包含用戶的表輸出流中的信息。如果我從命令promt手動運行查詢會話,我得到有關我的用戶的信息。 – MantiCore

0

How do you retrieve a list of logged-in/connected users in .NET?

修改了一下,以下代碼將列出登錄用戶及其相應的會話ID。

class Program 
{ 
    static void Main(string[] args) 
    { 
     var userLogins = new UserLogins(); 

     // this code gets the users from localhost - 
     // can change this to a remote hostname on the network 
     var users = UserLogins.GetUsers("localhost"); 

     foreach (var user in users) 
     { 
      Console.WriteLine("User: " + user); 
     } 

     Console.ReadKey(); 

    } 

    public class UserLogins 
    { 

     [DllImport("wtsapi32.dll")] 
     static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName); 

     [DllImport("wtsapi32.dll")] 
     static extern void WTSCloseServer(IntPtr hServer); 

     [DllImport("wtsapi32.dll")] 
     static extern Int32 WTSEnumerateSessions(
      IntPtr hServer, 
      [MarshalAs(UnmanagedType.U4)] Int32 Reserved, 
      [MarshalAs(UnmanagedType.U4)] Int32 Version, 
      ref IntPtr ppSessionInfo, 
      [MarshalAs(UnmanagedType.U4)] ref Int32 pCount); 

     [DllImport("wtsapi32.dll")] 
     static extern void WTSFreeMemory(IntPtr pMemory); 

     [DllImport("Wtsapi32.dll")] 
     static extern bool WTSQuerySessionInformation(
      System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned); 

     [StructLayout(LayoutKind.Sequential)] 
     private struct WTS_SESSION_INFO 
     { 
      public Int32 SessionID; 

      [MarshalAs(UnmanagedType.LPStr)] 
      public String pWinStationName; 

      public WTS_CONNECTSTATE_CLASS State; 
     } 

     public enum WTS_INFO_CLASS 
     { 
      WTSInitialProgram, 
      WTSApplicationName, 
      WTSWorkingDirectory, 
      WTSOEMId, 
      WTSSessionId, 
      WTSUserName, 
      WTSWinStationName, 
      WTSDomainName, 
      WTSConnectState, 
      WTSClientBuildNumber, 
      WTSClientName, 
      WTSClientDirectory, 
      WTSClientProductId, 
      WTSClientHardwareId, 
      WTSClientAddress, 
      WTSClientDisplay, 
      WTSClientProtocolType 
     } 
     public enum WTS_CONNECTSTATE_CLASS 
     { 
      WTSActive, 
      WTSConnected, 
      WTSConnectQuery, 
      WTSShadow, 
      WTSDisconnected, 
      WTSIdle, 
      WTSListen, 
      WTSReset, 
      WTSDown, 
      WTSInit 
     } 

     public static IntPtr OpenServer(String Name) 
     { 
      IntPtr server = WTSOpenServer(Name); 
      return server; 
     } 
     public static void CloseServer(IntPtr ServerHandle) 
     { 
      WTSCloseServer(ServerHandle); 
     } 
     public static IEnumerable<UserInfo> GetUsers(String ServerName) 
     { 
      IntPtr serverHandle = IntPtr.Zero; 
      List<String> resultList = new List<string>(); 
      serverHandle = OpenServer(ServerName); 

      try 
      { 
       IntPtr SessionInfoPtr = IntPtr.Zero; 
       IntPtr userPtr = IntPtr.Zero; 
       IntPtr domainPtr = IntPtr.Zero; 
       Int32 sessionCount = 0; 
       Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref SessionInfoPtr, ref sessionCount); 
       Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); 
       Int32 currentSession = (int)SessionInfoPtr; 
       uint bytes = 0; 

       if (retVal != 0) 
       { 
        for (int i = 0; i < sessionCount; i++) 
        { 
         WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO)); 
         currentSession += dataSize; 

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

         yield return new UserInfo 
         { 
          Domain = Marshal.PtrToStringAnsi(domainPtr), 
          User = Marshal.PtrToStringAnsi(userPtr), 
          SessionID = si.SessionID 
         }; 

         WTSFreeMemory(userPtr); 
         WTSFreeMemory(domainPtr); 
        } 

        WTSFreeMemory(SessionInfoPtr); 
       } 
      } 
      finally 
      { 
       CloseServer(serverHandle); 
      } 
     } 
    } 
    public class UserInfo 
    { 
     public string Domain { get; set; } 
     public string User { get; set; } 

     public int SessionID { get; set; } 

     public override string ToString() 
     { 
      return string.Format("{0}\\{1}: {2}", Domain, User, SessionID); 
     } 
    } 
} 
+0

我試過這段代碼,但是我最終如何訪問用戶會話ID。所有的信息存儲在「用戶」這裏:Console.WriteLine(「用戶:」+用戶);所以「用戶」也存儲了會話ID? – MantiCore

+0

好吧,我想出了自己。但是現在如果我在服務器上運行我的應用程序並通過遠程連接,則說沒有用戶登錄。 – MantiCore