【问题标题】:How to search all the user accounts in a domain in .NET?如何在.NET 中搜索域中的所有用户帐户?
【发布时间】:2012-05-14 05:33:47
【问题描述】:

如何在 .NET 中搜索域中的所有用户帐户?

不是域中的计算机名称,而是用户帐户,这是您用来登录 Windows 的。

【问题讨论】:

    标签: c# .net windows vb.net winapi


    【解决方案1】:

    您可以尝试以下方法。

    PrincipalContext ctx = new PrincipalContext(ContextType.Machine,Environment.MachineName);
    UserPrincipal user = new UserPrincipal(ctx);
    user.Name = "*";
    PrincipalSearcher ps = new PrincipalSearcher();
    ps.QueryFilter = user;
    PrincipalSearchResult<Principal> result = ps.FindAll();
    foreach (Principal p in result)
    {
        using (UserPrincipal up = (UserPrincipal)p)
        {
            MessageBox.Show(up.Name);
        }
    }
    

    或者这个:

    using System.Management;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                SelectQuery query = new SelectQuery("Win32_UserAccount");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                foreach (ManagementObject envVar in searcher.Get())
                {
                    Console.WriteLine("Username : {0}", envVar["Name"]);
                }
    
                Console.ReadLine();
            }
    

    另请参阅How to list all Windows Users。

    【讨论】:

    • 第二个适用于域还是仅适用于本地用户?
    • 对于那些专门在你的机器上的用户,你是本地用户
    【解决方案2】:

    你可以使用类似下面的东西:

    List<string> LdapUsers = new List<string>();
    
    if (String.IsNullOrWhiteSpace(domain))
    {
        string username = WindowsIdentity.GetCurrent().Name;
        domain = username.Substring(0, username.IndexOf("\\"));
    }
    
    PrincipalContext context;
    if (!String.IsNullOrWhiteSpace(user) && !String.IsNullOrWhiteSpace(password) && !String.IsNullOrWhiteSpace(domain))
        context = new PrincipalContext(ContextType.Domain, domain, user, password);
    if (!String.IsNullOrWhiteSpace(domain))
        context = new PrincipalContext(ContextType.Domain, domain);
    else
        context = new PrincipalContext(ContextType.Domain);
    UserPrincipal userP = new UserPrincipal(context);
    userP.Enabled = true;
    PrincipalSearcher pS = new PrincipalSearcher();
    pS.QueryFilter = userP;
    
    PrincipalSearchResult<Principal> result = pS.FindAll();
    foreach (Principal p in result)
        LdapUsers.Add(domain + "\\" + p.SamAccountName);
    

    【讨论】:

      【解决方案3】:

      另一种搜索域用户的方法:

      using System.DirectoryServices;
      using System.DirectoryServices.ActiveDirectory;
      
              using (var domain = Domain.GetCurrentDomain())
              using (var directoryEntry = domain.GetDirectoryEntry())
              using (var directorySearcher = new DirectorySearcher(directoryEntry, "(&(objectCategory=person)(objectClass=user))"))
              {
                  directorySearcher.PageSize = 1000;
      
                  using (var searchResults = directorySearcher.FindAll())
                  {
                      foreach (SearchResult searchResult in searchResults)
                      {
                          using (var userEntry = searchResult.GetDirectoryEntry())
                          {
                              Console.WriteLine(userEntry.Properties["cn"][0]);
                          }
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-05
        相关资源
        最近更新 更多