-1

感谢 marc_s 回来。

我尝试了以下代码:

public void GetInfo()
{
    try
    {
        //Object obj;
        DirectorySearcher search;
        DirectoryEntry entry;
        SearchResult result;
        String mailid = "";
        bool flag = false;

        entry = new DirectoryEntry(LDAPpath);//, Domainwithuser, password);

        search = new DirectorySearcher(entry);
        search.Filter = "CN=DistributionList1";

        int i = search.Filter.Length;

        string str = "", str1 = "";

        foreach (SearchResult AdObj in search.FindAll())
        {
            foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"])
            {
                str += Convert.ToString(objName) + "<Br>";
                int selIndex = objName.IndexOf("CN=") + 3;
                int selEnd = objName.IndexOf(",OU") - 3;
                str1 += objName.Substring(selIndex, selEnd).Replace("\\", "") + "<BR>";

                DirectorySearcher dsSearch = new DirectorySearcher(entry);
                dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", "");

                foreach (SearchResult rs in dsSearch.FindAll())
                {
                    str1 += "&lt;p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "&lt;/font></p>";
                }
            }
        }

        Response.Write("&lt;BR>" + str + "&lt;Br>" + str1 + "&lt;BR>");
    }
    catch (Exception ex)
    {
        Response.Write("--unable to fetch--<BR>" + ex.Message);
    }
}

在这里,search.findAll抛出一个DirectoryCOMException说法

语法中的无效 dn

我需要获取属于分发列表的所有电子邮件地址。

我确实尝试实施以下链接中详述的方法,但不幸的是它不起作用: https ://forums.asp.net/t/1224607.aspx?Displaying+Members+in+a+Distribution+List

任何帮助表示赞赏。

谢谢。

4

1 回答 1

1

您应该检查System.DirectoryServices.AccountManagement(S.DS.AM) 命名空间。这让你的生活比旧的、相当笨重的生活轻松多DirectorySearcher……

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context - limit to the OU you're interested in
// use this constructor if you want just the default domain, and search in the whole domain
//     using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null))
// or use this line here to define a *container* to search inside of 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "OU=YourOU,DC=YourCompany,DC=Com"))
{
    // find the group in question - this can be either a DL, or a security group - both should be found just fine
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

    // if found....
    if (group != null)
    {
       // iterate over members
       foreach (Principal p in group.GetMembers())
       {
           Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
           // do whatever you need to do to those members
       }
    }
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易!

在此处阅读更多信息:

另外:我强烈建议清楚而严格地保持数据的获取显示分开 - 不要将获取与 HTML 表示混合 - 这只是一个巨大的“泥球”样式代码 -推荐!

一个方法可以检索您需要的数据,例如 a List<UserPrincipal>(或者您也可以定义自己的类来保存您需要的数据),然后有第二个单独的方法从第一个方法获取此信息并对其进行迭代显示它。

于 2016-10-28T20:54:26.773 回答