2012-12-18 46 views
0

我需要連接到客戶端AD服務器以顯示所有用戶的信息。他們給了我以下信息: fqdn,netbios名稱和域控制器。這足以連接嗎?我需要在C#中連接到Active Directory需要什麼信息?

using (var context = new PrincipalContext(ContextType.Domain, "",)) 
using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) 
{ 
    foreach (var result in searcher.FindAll()) 
    { 
      DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; 
    } 
} 

謝謝!

+0

見我的答案在另一個[交](http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory/) –

回答

0

要通過C#連接,你需要這樣的事情:

DirectoryEntry child = new DirectoryEntry("LDAP://" + domainControllerName + "/" + 
     objectDn, userName, password); 

如果域控制器的名稱,對象域,用戶名和密碼,你應該是好去。

只是擡起頭來,你因爲沒有提到你以前嘗試過的任何東西而被低估了。

+0

謝謝瑞安。我會嘗試的。而且,你說得對,我應該提到我的嘗試。目前,我試圖讓下面的工作,但不知道要爲PrincipalContext放置什麼:使用(var上下文=新的PrincipalContext(ContextType.Domain,「,」,) {var searcher = new PrincipalSearcher(new UserPrincipal(context))) foreach(var resultsin searcher.FindAll()) DirectoryEntry de = result.GetUnderlyingObject()as DirectoryEntry; }'代碼' – user948060

+0

grrr,對不起。不知道如何正確地編寫代碼格式。 – user948060

1

我想瑞安正在向你展示老辦法。從你的代碼看起來你正在使用更新的類。

  // create a principal searcher for running a search operation 
     using (PrincipalSearcher pS = new PrincipalSearcher(uParams)) 
     { 
      // assign the query filter property for the principal object you created 
      // you can also pass the user principal in the PrincipalSearcher constructor 
      pS.QueryFilter = uParams; 

      // run the query 
      using (PrincipalSearchResult<Principal> results = pS.FindAll()) 
      { 
       foreach (Principal item in results) 
       { 
        UserPrincipal u = item as UserPrincipal; 
        list.Add(new MyCustomClass(u.UserPrincipalName) 
        { 
         Cn = u.Name, 
         Email = u.EmailAddress, 
         EmployeeId = u.EmployeeId, 
         NameFirst = u.GivenName, 
         NameLast = u.Surname, 
         ObjectSid = u.Sid.ToString(), 
         DistinguishedName = u.DistinguishedName, 
         SamAccount = u.SamAccountName 
        }); 
       } 
      } 
     } 

請注意,廣告仍然強加sometihng喜歡上你的查詢1500項限制,所以你可能會需要你的DirectoryEntry頂部發送到這樣的事情:

 /// <summary> 
    /// group member enumeration, simple and fast for large AD groups 
    /// </summary> 
    /// <param name="deGroup"></param> 
    /// <returns>list if distinguished names</returns> 
    public static List<string> GetMemberList(DirectoryEntry deGroup) 
    { 
     List<string> list = new List<string>(); 
     DirectoryEntry entry = deGroup; 

     uint rangeStep = 1000; 
     uint rangeLow = 0; 
     uint rangeHigh = rangeLow + (rangeStep - 1); 
     bool lastQuery = false; 
     bool quitLoop = false; 

     do 
     { 
      string attributeWithRange; 
      if (!lastQuery) 
      { 
       attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh); 
      } 
      else 
      { 
       attributeWithRange = String.Format("member;range={0}-*", rangeLow); 
      } 
      using (DirectorySearcher searcher = new DirectorySearcher(entry)) 
      { 
       searcher.Filter = "(objectClass=*)"; 
       //searcher.Filter = LdapObjectMgr.filterDisabledUsers; 

       searcher.PropertiesToLoad.Clear(); 
       searcher.PropertiesToLoad.Add(attributeWithRange); 
       SearchResult results = searcher.FindOne(); 
       foreach (string res in results.Properties.PropertyNames) 
       { 
        //list the property names 
        System.Diagnostics.Debug.WriteLine(res.ToString()); 
       } 

       if (results.Properties.Contains(attributeWithRange)) 
       { 
        foreach (object obj in results.Properties[attributeWithRange]) 
        { 
         //Console.WriteLine(obj.GetType()); 
         if (obj.GetType().Equals(typeof(System.String))) 
         { 
         } 
         else if (obj.GetType().Equals(typeof(System.Int32))) 
         { 
         } 
         //Console.WriteLine(obj.ToString()); 
         list.Add(obj.ToString()); 
        } 
        if (lastQuery) 
        { 
         quitLoop = true; 
        } 
       } 
       else 
       { 
        if (lastQuery == false) 
        { lastQuery = true; } 
        else 
        { quitLoop = true; } 
       } 
       if (!lastQuery) 
       { 
        rangeLow = rangeHigh + 1; 
        rangeHigh = rangeLow + (rangeStep - 1); 
       } 
      } 
     } 
     while (!quitLoop); 

     return list; 
    } 
相關問題