为了账号安全,请及时绑定邮箱和手机立即绑定

在 AD 搜索中反转过滤器

在 AD 搜索中反转过滤器

C#
忽然笑 2022-12-31 12:43:20
我想在 AD 中搜索名称不以前缀开头的所有用户。我应该怎么做?这不起作用using (var context = new PrincipalContext(ContextType.Domain, "my_do_main"))        {            UserPrincipal template = new UserPrincipal(context);            template.UserPrincipalName = "!my_prefix*"; //invertion NOT works            using (var searcher = new PrincipalSearcher(template))            {                foreach (var result in searcher.FindAll())                {                    var de = result.GetUnderlyingObject() as DirectoryEntry;                    Console.WriteLine(de.Properties["userPrincipalName"].Value);                }            }        }
查看完整描述

1 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

你不能使用 来做到这一点PrincipalSearcher,但你可以使用 来做到这一点DirectorySearcher,无论如何这就是PrincipalSearcher幕后的用途。这是一个简单的例子:


var search = new DirectorySearcher(new DirectoryEntry("LDAP://my_do_main")) {

    PageSize = 1000,

    Filter = "(&(objectClass=user)(!userPrincipalName=my_prefix*))"

};

search.PropertiesToLoad.Add("userPrincipalName");


using (var results = search.FindAll()) {

    foreach (SearchResult result in results) {

         Console.WriteLine((string) result.Properties["userPrincipalName"][0]);

    }

}

你会发现这无论如何都会执行得更快。根据我的经验,直接使用DirectorySearcherDirectoryEntry总是比使用PrincipalSearcher(或AccountManagement名称空间中的任何东西)快得多。不久前我写了一篇关于该主题的文章:Active Directory:更好的性能


查看完整回答
反对 回复 2022-12-31
  • 1 回答
  • 0 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号