【问题标题】:How to search Global Catalog (whole forest) using PrincipalContext如何使用 PrincipalContext 搜索全局目录(整个森林)
【发布时间】:2016-07-14 12:08:58
【问题描述】:
 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 for users in Global Catalog within AD forest with multiple trees,但发帖人似乎暗示他们没有找到使用 PrincipalContext 类的解决方案,他们不得不切换回 DirectorySearcher。

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

【问题讨论】:

    标签: c# active-directory directoryservices directoryentry account-management


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      相关资源
      最近更新 更多