首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在asp.net c# web窗体应用程序中创建Active Directory OU中所有电子邮件地址的数组?

如何在asp.net c# web窗体应用程序中创建Active Directory OU中所有电子邮件地址的数组?
EN

Stack Overflow用户
提问于 2015-09-11 19:24:29
回答 2查看 318关注 0票数 1

我读过几篇文章,但我一直没能为自己找到任何东西。我想在“标准用户”OU中创建所有显示名称的数组。然后,我将使用数组列表来填充下拉列表。我在另一个线程中发现了以下内容,但它在指定的OU中提供组而不是用户:

代码语言:javascript
复制
public ArrayList Groups()
{
    ArrayList groups = new ArrayList();
    foreach (System.Security.Principal.IdentityReference group in
        System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
    {
        groups.Add(group.Translate(typeof
            (System.Security.Principal.NTAccount)).ToString());
    }

    return groups;
}

是否可以修改它以提供标准用户OU中的用户列表?还是我应该用另一种方法?

我也发现了这一点,但是我得到了一个错误(“std_users”下面的红线),它声明“并非所有的代码路径都返回一个值”。

代码语言:javascript
复制
public ArrayList std_users()
{
    // List of strings for your names
    List<string> allUsers = new List<string>();

    // create your domain context and define the OU container to search in
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "myco.org", "OU=Standard Users, dc=myco, dc=org");

    // define a "query-by-example" principal - here, we search for a UserPrincipal (user)
    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.....          
        allUsers.Add(found.DisplayName);
    }
}

任何帮助都将不胜感激。

我使用的是c#,Visual 2013,框架为4.5.1。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-11 19:46:17

return allUsers;添加到std_users()中,并让我们知道这是否适合您。

票数 -1
EN

Stack Overflow用户

发布于 2015-09-11 19:56:36

首先:停止使用 ArrayList --该类型自.NET 2.0以来就是dead,不应该使用--使用List<string> (或者更一般地说:List<T>) --性能和可用性都要好得多!

您可以使用PrincipalSearcher和“逐例查询”主体来执行搜索:

代码语言:javascript
复制
public List<string> GetAllEmailsFromUsersContainer()
{
    List<string> users = new List<string>();

    // create your domain context and bind to the standard CN=Users
    // container to get all "standard" users
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "CN=Users,dc=YourCompany,dc=com"))
    {
        // define a "query-by-example" principal - here, we search for a UserPrincipal 
        // which is not locked out, and has an e-mail address   
        UserPrincipal qbeUser = new UserPrincipal(ctx);
        qbeUser.IsAccountLockedOut = false;
        qbeUser.EmailAddress = "*";

        // create your principal searcher passing in the QBE principal    
        PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

        // find all matches
        foreach(var found in srch.FindAll())
        {
            users.Add(found.EmailAddress);
        }
    }

    return users;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32530779

复制
相关文章

相似问题

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