C#DirectoryEntry找到具有特定属性的所有用户(wwwhomepage)

发布于 2025-02-12 20:26:01 字数 500 浏览 2 评论 0原文

C#中使用Directory输入的最佳方法是找到所有属性wwwhomepage的用户。

我能够查看特定用户是否拥有它,但我尚未使用目录条目来搜索所有用户以搜索所有用户。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, myDomain, Login.authUserName, Login.authPassword);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if (user != null) {
    DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
    if (de != null) {
        string whatIWant = de.Properties["wWWHomePage"].Value.ToString();
    }
}

What would be the best way in C# to use directory entry to find all users with the attribute wWWHomePage filled in.

I am able to see if a specific user has it but I have not used Directory Entry to search all users for something like this.

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, myDomain, Login.authUserName, Login.authPassword);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if (user != null) {
    DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
    if (de != null) {
        string whatIWant = de.Properties["wWWHomePage"].Value.ToString();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2025-02-19 20:26:01

使用DirectoryEntryDirectorySearcher并指定搜索过滤器以获取所需的东西。

过滤器您想要的模板是:

(&(objectClass=user)(objectCategory=person)(PROPERTY_NAME=SEARCH_TERM))

其中property_name是您要搜索的属性,而search_term是值。您可以将*用作通配符搜索,它将为您提供具有此属性的所有对象。

这是一个快速示例:

// set the properties you need.
var propertiesToLoad = new string[] { "sAMAccountName", "wWWHomePage" };

using(var searcher = new DirectorySearcher(new DirectoryEntry(ldap), "(&(objectClass=user)(objectCategory=person)(wWWHomePage=*))", propertiesToLoad))
{
    foreach (SearchResult result in searcher.FindAll())
    {
        if(result == null) continue;
    
        var samAccount = result.Properties["sAMAccountName"][0];
        
        var wWWHomePage = result.Properties["wWWHomePage"][0];
        
        // complete the code with your logic  
    }
}

use DirectoryEntry with DirectorySearcher and specify the search Filter to get what you want.

the Filter template you want is :

(&(objectClass=user)(objectCategory=person)(PROPERTY_NAME=SEARCH_TERM))

where PROPERTY_NAME is the property you want to search in, and SEARCH_TERM is the value. you could use the * as a wildcard search, it would give you all objects that has this property.

here is a quick example :

// set the properties you need.
var propertiesToLoad = new string[] { "sAMAccountName", "wWWHomePage" };

using(var searcher = new DirectorySearcher(new DirectoryEntry(ldap), "(&(objectClass=user)(objectCategory=person)(wWWHomePage=*))", propertiesToLoad))
{
    foreach (SearchResult result in searcher.FindAll())
    {
        if(result == null) continue;
    
        var samAccount = result.Properties["sAMAccountName"][0];
        
        var wWWHomePage = result.Properties["wWWHomePage"][0];
        
        // complete the code with your logic  
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文