如何使用PrincipalContext搜索全局编录(整个林)

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中搜索用户信息。

正如您所看到的,我在搜索过程中传入了domainName。 我如何修改这个代码的和平来代替搜索整个森林(即全局目录),但仍然使用PrincipalContext类?

我似乎无法找到一个使用PrincipalContext类进行全局编录搜索的工作示例。

我已经看过这篇文章如何在AD森林中搜索具有多个树的全局目录中的用户,但是海报似乎表明他们没有找到使用PrincipalContext类的解决方案,他们不得不切换回DirectorySearcher。

是否有任何PrincipalContext类代码示例演示在整个林中进行搜索(全局编录)?

好的,我搞定了。 我只需要像下面那样更改我的代码。

  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); } } 
Interesting Posts