【问题标题】:Active Directory DirectorySearcher is not returning all of the available propertiesActive Directory DirectorySearcher 未返回所有可用属性
【发布时间】:2012-12-03 16:29:22
【问题描述】:

我正在为我的老板编写一个新程序,以取代他们目前使用的旧 VBS。

所以程序假设进入广告并收集所有员工的姓名和他们的电子邮件地址。我的问题是每个用户都有大约 60 个属性分配给他们,但我的程序只提取 32 个字段,其中一个是 CN,它是我需要的一半。当然,邮件不是要导入的属性之一。我在调试时也注意到,我认为只是从长岛分支机构引进员工,而不是从我不明白为什么的任何地方引进员工。任何帮助将不胜感激!! =D

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using Microsoft.Office.Interop.Excel;
using System.DirectoryServices.ActiveDirectory; 


namespace EmailListing
{
    class Program
    {
        static void Main(string[] args)
        {


            DirectoryEntry adFolderObject = new DirectoryEntry("LDAP://OU=PHF Users,DC=phf,DC=inc");


            DirectorySearcher adSearchObject = new DirectorySearcher(adFolderObject);
            adSearchObject.SearchScope = SearchScope.Subtree;



            adSearchObject.Filter = "(&(ObjectClass=user)(!description=Built-in*))";




            foreach (SearchResult adObject in adSearchObject.FindAll())
             {
                 //mail = adObject.Properties["mail"].ToString();

                Console.Write(adObject.Properties["cn"][0]); 
                Console.Write(".        ");
                //Console.WriteLine(mail);





             }

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    您可以使用PrincipalSearcher 和“示例查询”主体进行搜索:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
        UserPrincipal foundUser = found as UserPrincipal;
    
        if (foundUser != null && !foundUser.Description.StartsWith("Built-In"))
        {
            string firstName = foundUser.GivenName;
            string lastName = foundUser.Surname;
            string email = foundUser.EmailAddress;
        }
    }
    

    如果您还没有 - 一定要阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能。或查看MSDN documentation on the System.DirectoryServices.AccountManagement 命名空间。

    当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:

    • DisplayName(通常:名字 + 空格 + 姓氏)
    • SAM Account Name - 您的 Windows/AD 帐户名
    • User Principal Name - 您的“username@yourcompany.com”样式名称

    您可以在UserPrincipal 上指定任何属性并将其用作PrincipalSearcher 的“示例查询”。

    【讨论】:

    • 首先我想说我爱你!我一直在到处搜索,上论坛,youtube,什么都没有。这行得通!获取电子邮件地址和姓名。谢谢你!最后一个问题我如何过滤它以仅拥有用户。当我添加 if 语句时出现错误,if 语句是过滤器吗?
    • @user1873179:通过将UserPrincipal 类指定为“搜索参数”(qbeUser),您就是在告诉 S.DS.AM 仅搜索用户。
    • 有没有办法获取具有电子邮件地址的帐户。我看到的是姓名,没有电子邮件地址。我希望只显示具有电子邮件地址的用户。
    • 想通了,您只需将要过滤的任何内容放入 if statame 中,即。 foundUser.EmailAddress != null
    • @user1873179:正是——这就是它被称为 query-by-example 的原因——您只需提供您正在寻找的示例(或模板)...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多