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

How to search Global Catalog (whole forest) using PrincipalContext(如何使用 PrincipalContext 搜索全局目录(整个森林))
本文介绍了如何使用 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 搜索全局目录(整个森林)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Working with DirectoryServices in ASP.NET Core(在 ASP.NET Core 中使用 DirectoryServices)
Create Active Directory user in .NET (C#)(在 .NET (C#) 中创建 Active Directory 用户)