答えて

2

これを行う1つの方法は、System.DirectoryServices.DirectorySearcherを使用して、アクティブディレクトリからのユーザーのリストを尋ねることです。

更新

ここで我々が使用するクラスです。 FetchContacts静的メソッドを呼び出してユーザーを取得することができます。

using System; 
using System.Collections.Generic; 
using System.DirectoryServices; 

public class ActiveDirectorySystem 
{ 
    private const string PROPERTY_DISPLAY_NAME = "displayName"; 

    private static DirectorySearcher Initialize() 
    { 
     // Exceptions are handled by the caller 

     // Obtain the domain root entry 
     using (DirectoryEntry theRootEntry = new DirectoryEntry("LDAP://RootDSE")) 
     { 
      object theNamingContext = null; 
      string sNamingContext = null; 

      // Verify that we retrieved it correctly and raise an error if we did not 
      if (theRootEntry == null) 
      { 
       throw new Exception("A directory services entry for the LDAP RootDSE could not be created."); 
      } 

      // Get the root naming context 
      theNamingContext = theRootEntry.Properties["rootDomainNamingContext"].Value; 
      // Verify that we retrieved it correctly and raise an error if we did not 
      if ((theNamingContext == null) || (theNamingContext.ToString().Length == 0)) 
      { 
       throw new Exception("The root domain naming context property could not be retrieved from the LDAP directory services"); 
      } 
      else 
      { 
       sNamingContext = theNamingContext.ToString(); 
      } 

      // And create a new directory entry for the root naming context 
      using (DirectoryEntry theEntry = new DirectoryEntry("LDAP://" + sNamingContext)) 
      { 
       // Verify that we retrieved it correctly and raise an error if we did not 
       if (theEntry == null) 
       { 
        throw new Exception("A directory entry object could not be created for LDAP://" + sNamingContext); 
       } 

       // Now we configure what we are looking for from Active Directory 
       DirectorySearcher oSearcher = null; 

       // Start with a new searcher for the root domain 
       oSearcher = new DirectorySearcher(theEntry); 
       // Verify that we retrieved it correctly and raise an error if we did not 
       if (oSearcher == null) 
       { 
        throw new Exception("A directory searcher object could not be created for LDAP://" + sNamingContext); 
       } 

       // And the properties we want to retrieve 
       oSearcher.PropertiesToLoad.Add(PROPERTY_DISPLAY_NAME); 

       return oSearcher; 
      } 
     } 
    } 

    /// <summary> 
    /// This method retrieves the set of users from active directory that meet the criteria specified in filter. 
    /// </summary> 
    /// <param name="sFilter"></param> 
    /// <returns></returns> 
    /// <remarks></remarks> 
    public static List<string> FetchContacts(string sFilter = "") 
    { 
     // Exceptions are handled by the caller 

     using (DirectorySearcher oSearcher = Initialize()) 
     { 
      if (oSearcher != null) 
      { 
       List<string> cNames = new List<string>(); 

       // Specify what we are looking for, which is the account name of the specified user without any domain information 
       oSearcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user){0})", sFilter); 

       // get all of the matching records 
       using (SearchResultCollection cResults = oSearcher.FindAll()) 
       { 
        if (cResults != null) 
        { 
         foreach (SearchResult theCurrentResult in cResults) 
         { 
          System.DirectoryServices.ResultPropertyCollection oProperties = null; 

          oProperties = theCurrentResult.Properties; 
          // First, verify that at least the display name is contained in the result 
          if (oProperties.Contains(PROPERTY_DISPLAY_NAME) && oProperties[PROPERTY_DISPLAY_NAME].Count > 0) 
          { 
           cNames.Add(oProperties[PROPERTY_DISPLAY_NAME][0].ToString()); 
          } 
         } 
        } 
       } 

       return cNames; 
      } 
     } 

     return null; 
    } 
} 
+0

私は実際にデータを取得する方法を理解していません。例はありますか?ありがとう。 – Ber53rker

+0

@ Ber53rker:私たちが使っているクラスのカットダウン版で答えを更新しました。 –

+0

甘い!ありがとう!それは働いています、一種。今私は特定のグループからの人を取得する方法を理解する必要があります... – Ber53rker

関連する問題