0
myUserList AppUsers = new myUserList();  
using (PrincipalContext pcxt = new PrincipalContext(ContextType.Domain, domainName)) 
      { 
       UserPrincipal User = new UserPrincipal(pcxt); 
       User.EmailAddress = emailString; 

       PrincipalSearcher srch = new PrincipalSearcher(User); 
       foreach (var principal in srch.FindAll()) 
       { 
        var p = (UserPrincipal)principal; 
        myUserRow User = AppUsers.NewUsersRow(); 
        User.FirstName = p.GivenName; 
        User.LastName = p.Surname; 
        User.Email = p.EmailAddress; 
        AppUsers.AddUsersRow(User); 

       } 
      } 

我有類似於上面的代碼,使用PrincipalContext類在Active Directory中搜索用戶信息。如何使用PrincipalContext搜索全局編錄(整個林)

正如你所看到的,我在搜索過程中傳入了domainName。 如何修改代碼的和平來代替搜索整個森林(即全局目錄),但仍然使用PrincipalContext類?

我似乎無法找到一個使用PrincipalContext類來執行全局編錄搜索的工作示例。

我看到這個帖子How to search for users in Global Catalog within AD forest with multiple trees但是這張海報似乎暗示他們沒有找到使用PrincipalContext類的解決方案,他們不得不切換回DirectorySearcher。

是否有任何PrincipalContext類代碼示例演示在整個林(全局編錄)中搜索?

回答

0

好吧,我知道它的工作。我只需要像下面那樣更改我的代碼。

myUserList AppUsers = new myUserList();  
using (PrincipalContext pcxt = new PrincipalContext(ContextType.Domain, "my-global-catalogue-server.subdomain.domain.com:port", "DC=subdomain,DC=domain,DC=com")) 
      { 
       UserPrincipal User = new UserPrincipal(pcxt); 
       User.EmailAddress = emailString; 

       PrincipalSearcher srch = new PrincipalSearcher(User); 
       foreach (var principal in srch.FindAll()) 
       { 
        var p = (UserPrincipal)principal; 
        myUserRow User = AppUsers.NewUsersRow(); 
        User.FirstName = p.GivenName; 
        User.LastName = p.Surname; 
        User.Email = p.EmailAddress; 
        AppUsers.AddUsersRow(User); 

       } 
      }