首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要使用C#的当前组织中的所有用户详细信息(姓名、电子邮件、指定名称、部门)

需要使用C#的当前组织中的所有用户详细信息(姓名、电子邮件、指定名称、部门)
EN

Stack Overflow用户
提问于 2016-10-25 09:22:49
回答 1查看 346关注 0票数 0

我跟踪了C# LDAP query to retrieve all users in an organisational unit"A referral was returned from the server" exception when accessing AD from C#的链接

我需要知道我在LDAP路径上做错了什么?

代码语言:javascript
复制
 // create your domain context and define what container to search in - here OU=Employees
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MS", "OU=Employees,DC=CompanyName,DC=com");

        // define a "query-by-example" principal - here, we search for a UserPrincipal 
        // that is still active
        UserPrincipal qbeUser = new UserPrincipal(ctx);
        qbeUser.Enabled = true;

        // 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.....          
        }

我需要所有用户的详细信息(姓名,电子邮件,指定,部门)在目前的组织使用C#和显示在下拉列表。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-18 09:24:48

代码语言:javascript
复制
public DataTable FindPersons(string lname, string fname)
    { 

        DirectorySearcher searcher = new DirectorySearcher();
        searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0}*)(sn={1}*))", fname, lname);

        SearchResultCollection allResults;
        allResults = searcher.FindAll();

        DataTable dt = new DataTable();
        dt.Columns.Add("DisplayName", typeof(string));
        dt.Columns.Add("GivenName", typeof(string));
        dt.Columns.Add("SurName", typeof(string));
        dt.Columns.Add("MSID", typeof(string));
        if (allResults.Count >= 0)
        { 
            for (int i = 0; i < allResults.Count; i++)
            {
                DirectoryEntry deMembershipUser = allResults[i].GetDirectoryEntry();
                deMembershipUser.RefreshCache(); 

                dt.Rows.Add(
                    (string)deMembershipUser.Properties["displayname"].Value, 
                    (string)deMembershipUser.Properties["givenName"].Value,
                    (string)deMembershipUser.Properties["sn"].Value, 
                    (string)deMembershipUser.Properties["cn"].Value
                    ); 
            } 
        } 
        return dt;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40236144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档