问题
In a C# Windows Forms Application, I'm trying to get a list of the users currently logged into a workstation (both local and domain users). Every search about this has led me to threads mentioning "just use LsaEnumerateLogonSessions".
So...how do you actually use this? The MSDN page is very sparse and doesn't seem to offer any clues.
回答1:
You should use Cassia, an open source wrapper.
ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("your-server-name"))
{
server.Open();
foreach (ITerminalServicesSession session in server.GetSessions())
{
Console.WriteLine("Session ID: " + session.SessionId);
Console.WriteLine("User: " + session.UserAccount);
Console.WriteLine("State: " + session.ConnectionState);
Console.WriteLine("Logon Time: " + session.LoginTime);
}
}
I'm not sure how this will handle domain users; try it in LINQPad.
To answer your question, you need to declare it as a P/Invoke method that takes an out in and an out long[].
回答2:
This article shows how to use LsaEnumerateLogonSessons:
http://www.codeproject.com/Articles/18179/Using-the-Local-Security-Authority-to-Enumerate-Us
来源:https://stackoverflow.com/questions/5669899/how-do-you-use-lsaenumeratelogonsessions-in-c