如何使用 PrincipalContext 搜索全局目录(整个森林

时间:2023-02-18
本文介绍了如何使用 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 中搜索用户信息.

I have code similar to the above that searches Active Directory for user information using PrincipalContext class.

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

As you can see i pass in the domainName during the search. How can i modify this peace of code to instead search the entire forest (i.e Global Catalog) but still use the PrincipalContext class?

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

I can't seem to find a working example that uses PrincipalContext class to do a Global Catalog search.

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

I have seen this post How to search for users in Global Catalog within AD forest with multiple trees but the poster seems to suggest that they did not find a solution that uses PrincipalContext class, they had to switch back to DirectorySearcher.

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

Is there any PrincipalContext class code sample that demonstrates searching in the whole forest (Global Catalog)?

推荐答案

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

ok, i got it working. I just had to change my code like below.

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

                }
            }

这篇关于如何使用 PrincipalContext 搜索全局目录(整个森林)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:使用 SSL 联系 Active Directory 下一篇:使用 Azure Active Directory 对用户进行身份验证后执

相关文章

最新文章