How do you use LsaEnumerateLogonSessions in C#?

那年仲夏 提交于 2019-12-11 23:39:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!