c# - DirectorySearcher:如何仅获取“真实”用户的条目
问题描述
我使用 aDirectorySearcher
从 Active Directory 中获取所有用户 - 但我只需要获取“真实”用户。
筛选:
search.Filter = "(&(objectClass=user)(objectCategory=person))";
但我得到了所有用户帐户,例如:
henry.miller <-wanted
ernest.hemingway <-wanted
HealthMailboxced7671 <-not wanted
问题:如何修改我的过滤器以仅返回真实用户?
我的整个代码:
string DomainPath = "LDAP://DC=writers,DC=local";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("samaccountname"))
{
Console.WriteLine((String)result.Properties["samaccountname"][0]);
}
}
}
解决方案
在下面尝试使用 LDAP
(&(objectCategory=person)(objectClass=user)(sAMAccountName=*)(!(cn=*O*)))
仅供参考 - CN 是通用名称。您可以在此处获取有关 LDAP 的更多信息
您还问我可以用什么属性检索 cn。在这里,您可以通过此代码片段提取(用于双重检查)。但据我所知,它只是 CN
foreach (string property in result.Properties.PropertyNames)
{
foreach (Object propertyValue in result.Properties[property])
{
// print out the Property Value here
}
}
推荐阅读
- amazon-web-services - 从 Redshift Spectrum 获取“磁盘已满”错误
- c++ - 使用 gcc 8.2.1 对构造函数进行模棱两可的调用
- python - 将我的 Python 代码输出到 csv 文件
- javascript - VSCode 调试外部 npm 库
- kotlin - 按第二个值对对进行分组,并将其映射到键是第二个元素,值是与第二个元素关联的第一个元素的列表
- php - 如何制作像 Facebook 一样的照片库?
- c - putchar 和整个代码是如何执行的?
- javascript - 在 WebGL 中使用 gl.POINTS 绘制点时如何忽略间隙?
- python - 获取我在列表中搜索的第二个元素
- sql - 在另一个存储过程中使用一个存储过程的结果 - SQL Server