1
 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 类?

我似乎找不到使用 PrincipalContext 类进行全局目录搜索的工作示例。

我看过这篇文章How to search users in Global Catalog within AD forest with multiple trees,但发帖者似乎暗示他们没有找到使用 PrincipalContext 类的解决方案,他们不得不切换回 DirectorySearcher。

是否有任何 PrincipalContext 类代码示例演示了在整个森林(全局目录)中的搜索?

4

1 回答 1

2

好的,我让它工作了。我只需要像下面这样更改我的代码。

 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);

                }
            }
于 2016-07-14T14:14:03.040 回答