How to search Global Catalog (whole forest) using 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 搜索全局目录(整个森林)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PrincipalContext 搜索全局目录(整个森林
基础教程推荐
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
