加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用远程计算机上的状态登录用户

发布时间:2020-12-15 08:12:34 所属栏目:百科 来源:网络整理
导读:我正在寻找一种方法来获取在远程计算机上登录的用户.我很想知道他们是在当地还是远程登录,但最重要的是我必须知道他们的状态. 我在网上看到了一些用VB编写的答案,但我需要在c#中使用它. 在 markdmak answer here中给出的解决方案看起来是一个好的开始,但它在
我正在寻找一种方法来获取在远程计算机上登录的用户.我很想知道他们是在当地还是远程登录,但最重要的是我必须知道他们的状态.
我在网上看到了一些用VB编写的答案,但我需要在c#中使用它.
在 markdmak answer here中给出的解决方案看起来是一个好的开始,但它在VB中,它只查找远程会话.
我有这段代码,这可能是一个开始,但我想将LogonId耦合到用户名并查看其状态:
string fqdn = ""; // set!!!    
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
// To connect to the remote computer using a different account,specify these values:
// these are needed in dev environment
options.Username = ConfigurationManager.AppSettings["KerberosImpersonationUser"];
options.Password = ConfigurationManager.AppSettings["KerberosImpersonationPassword"];
options.Authority = "ntlmdomain:" + ConfigurationManager.AppSettings["KerberosImpersonationDomain"];

ManagementScope scope = new ManagementScope("\" + fqdn + "rootCIMV2",options);
try
{
    scope.Connect();
}
catch (Exception ex)
{
    if (ex.Message.StartsWith("The RPC server is unavailable"))
    {
        // The Remote Procedure Call server is unavailable
        // cannot check for logged on users
        return false;
    }
    else
    {
        throw ex;
    }
}

SelectQuery query = new SelectQuery("Select * from Win32_LogonSession");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);
ManagementObjectCollection results = searcher.Get();
bool returnVal = false;
foreach (ManagementObject os in results)
{
    try
    {
        if (os.GetPropertyValue("LogonId").ToString() != null && os.GetPropertyValue("LogonId").ToString() != "")
        {
            returnVal = true;
        }
    }
    catch (NullReferenceException)
    {
        continue;
    }
}
return returnVal;
}

我真正需要和找不到的是一种让远程机器上的所有用户及其状态的方式,即:活动,断开连接,注销等.

解决方法

您可以对 LogonType属性使用Win32_LogonSession WMI类筛选,值为2(交互式)

试试这个样本

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
class Program
{

    static void Main(string[] args)
    {
        try
        {
            string ComputerName = "remote-machine";
            ManagementScope Scope;

            if (!ComputerName.Equals("localhost",StringComparison.OrdinalIgnoreCase))
            {
                ConnectionOptions Conn = new ConnectionOptions();
                Conn.Username = "username";
                Conn.Password = "password";
                Conn.Authority = "ntlmdomain:DOMAIN";
                Scope = new ManagementScope(String.Format("\{0}rootCIMV2",ComputerName),Conn);
            }
            else
                Scope = new ManagementScope(String.Format("\{0}rootCIMV2",null);

            Scope.Connect();
            ObjectQuery Query = new ObjectQuery("SELECT LogonId  FROM Win32_LogonSession Where LogonType=2");
            ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope,Query);

            foreach (ManagementObject WmiObject in Searcher.Get())
            {
                Console.WriteLine("{0,-35} {1,-40}","LogonId",WmiObject["LogonId"]);// String
                ObjectQuery LQuery = new ObjectQuery("Associators of {Win32_LogonSession.LogonId=" + WmiObject["LogonId"] + "} Where AssocClass=Win32_LoggedOnUser Role=Dependent");
                ManagementObjectSearcher LSearcher = new ManagementObjectSearcher(Scope,LQuery);
                foreach (ManagementObject LWmiObject in LSearcher.Get())
                {
                    Console.WriteLine("{0,"Name",LWmiObject["Name"]);                    
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
        }
        Console.WriteLine("Press Enter to exit");
        Console.Read();
    }
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读