简体   繁体   English

C#,如何获取基于查询的通讯组的成员? (活动目录)

[英]C#, how I can get the members of a query-based distribution group ? (Active Directory)

I can't get the users of a query-based distribution group in ac# console application.我无法在 ac# 控制台应用程序中获取基于查询的通讯组的用户。 I was searching for some posts like this C# Get Exchange Distribution and Dynamic Distribution Lists and their members , but I don't see the distribution group in the property "memberOf".我正在搜索一些类似C# Get Exchange Distribution and Dynamic Distribution Lists and their members的帖子,但我没有在属性“memberOf”中看到通讯组。 In this property, I can see "Groups", but not "query-based distribution group".在此属性中,我可以看到“组”,但看不到“基于查询的通讯组”。

Thanks.谢谢。

Solution C# Get Exchange Distribution and Dynamic Distribution Lists and their members解决方案C# 获取 Exchange 分发和动态分发列表及其成员

Edit Class编辑类

public class DistributionList
{
    public DLT DType { get; set; }
    public string CN { get; set; }
    public string FILORDN { get; set; }
    public List<string> Members { get; set; }
}

In Main在主要

static void Main(string[] args)
{
    var distributionLists = GetDynamicDistributionLists();

    foreach (var distributionList in distributionLists)
    {
        distributionList.Members = listDDLMembers(distributionList.FILORDN);
    }
}

According to the article you linked, Exchange dynamic groups membership must be retrieved in two steps.根据您链接的文章,必须分两步检索 Exchange 动态组成员身份。

  1. You must retrieve the directory entry for the dynamic distribution list so you can get the msExchDynamicDLFilter property.您必须检索动态通讯组列表的目录条目,以便您可以获得 msExchDynamicDLFilter 属性。

  2. You must use the msExchDynamicDLFilter property in a user search to find all users matching the property.您必须在用户搜索中使用 msExchDynamicDLFilter 属性来查找与该属性匹配的所有用户。

There are very few articles about Dynamic Distribution Group Search etc. on the web.网上关于动态通讯组搜索等的文章很少。 Additionally the web page that pointed as solution looks dead unfortunately.此外,不幸的是,指向解决方案的网页看起来已经死了。 Fortunately, I found lost article by waybackmachine tool and its content can be useful for people like me so I place it here;幸运的是,我通过waybackmachine 工具找到了丢失的文章,它的内容对我这样的人有用,所以我把它放在这里;


Below is the function which enables you todo so but before that we need to create a common class for both distribution list because they have something in common which is users who has email address.以下是使您能够这样做的功能,但在此之前我们需要为两个分发列表创建一个公共类,因为它们有一些共同点,即拥有电子邮件地址的用户。 normal distribution list has members while the dynamic distribution list members is being auto generated based on LDAP Query.普通分发列表有成员,而动态分发列表成员是根据 LDAP 查询自动生成的。

first the common Class首先是普通类

/// <summary>
/// This enum will hold the type of a Distrinution List 
/// DL for Normal Distribution List 
/// DLL Dynamic Distribution List
/// </summary>
public enum DLT { DL = 1, DDL = 2 }

/// <summary>
/// This Class will Hold all the information about DL or DDL 
/// and based on the DLT it will call the disignated function to return the list of email address
/// </summary>
public class DistributionList
{
    public DLT DType { get; set; }
    public string CN { get; set; }
    public string FILORDN { get; set; }
}

Then the code for every type of distribution list I hope that you will find a good use for that然后是每种类型的分发列表的代码,我希望你能找到一个很好的用途

/// <summary>
/// Get the List of A dynamic distribution Group in Key Value Pairs 
/// the key is the CN
/// the value is the filter for the membership of that group
/// </summary>
/// <returns>Dictionary of strings </returns>
public List<DistributionList> GetDynamicDistributionLists() 
{
    List<DistributionList> distrubutionLists = new List<DistributionList>();

    using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
    {
        
        using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
        using (var searcher = new DirectorySearcher(searchRoot, "(ObjectClass=msExchDynamicDistributionList)"))
        using (var results = searcher.FindAll())
        {
            foreach (SearchResult result in results)
            {
                if (result.Properties.Contains("cn") && result.Properties.Contains("msExchDynamicDLFilter"))
                {
                    DistributionList dl = new DistributionList();
                    
                    dl.DType = DLT.DDL;
                    dl.CN = result.Properties["cn"][0].ToString();
                    dl.FILORDN = result.Properties["msExchDynamicDLFilter"][0].ToString();

                    distrubutionLists.Add(dl); 
                }
            }
        }

    }

    return distrubutionLists;

}

/// <summary>
/// Get the List of A distribution Group in Key Value Pairs
/// the key is the CN
/// the value is the DN of the Group
/// </summary>
/// <returns>Dictionary of strings</returns>
public List<DistributionList> GetDistributionLists() 
{
    List<DistributionList> distrubutionLists = new List<DistributionList>();

    using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
    {

        using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
        using (var searcher = new DirectorySearcher(searchRoot, "(&(objectCategory=group)(!groupType:1.2.840.113556.1.4.803:=2147483648))"))
        using (var results = searcher.FindAll())
        {
            foreach (SearchResult result in results)
            {
                if (result.Properties.Contains("cn") && result.Properties.Contains("distinguishedName"))
                {
                    DistributionList dl = new DistributionList();
                    
                    dl.DType = DLT.DL;
                    dl.CN = result.Properties["cn"][0].ToString();
                    dl.FILORDN = result.Properties["distinguishedName"][0].ToString();

                    distrubutionLists.Add(dl);
                }
            }
        }

    }

    return distrubutionLists;
}

/// <summary>
/// List Distribution List Members
/// </summary>
/// <param name="dn"> The distinguishedName of the Group </param>
/// <returns>List of Email Addresses</returns>
public List<string> listDLMembers(string dn) 
{
    List<string> addresses = new List<string>();

    using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
    {

        using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
        using (var searcher = new DirectorySearcher(searchRoot, "(&(objectCategory=person)(|(objectClass=contact)(objectClass=user))(memberOf=" + dn + "))"))
        using (var results = searcher.FindAll())
        {
            foreach (SearchResult result in results)
            {
                if (result.Properties.Contains("mail"))
                    addresses.Add(result.Properties["mail"][0].ToString());
            }
        }
    }
    return addresses;
}

/// <summary>
/// List Dynamic Distribution List Members
/// </summary>
/// <param name="filter"> Filter to be used in the LDAP Query</param>
/// <returns>List Of Email Addresses</returns>
public  List<string>  listDDLMembers(string filter) 
{
   // The filter is the value of the previous dictionary

    List<string> addresses = new List<string>();

    using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
    {

        using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
        using (var searcher = new DirectorySearcher(searchRoot, filter))
        using (var results = searcher.FindAll())
        {
            foreach (SearchResult result in results)
            {
                if (result.Properties.Contains("mail"))
                    addresses.Add(result.Properties["mail"][0].ToString());
            }
        }
    }
    return addresses;
}

And a little conversation under the article;并在文章下进行了一些对话;

Q

In your method listDDLMembers what would the ldap query look like?在您的方法 listDDLMembers 中,ldap 查询会是什么样子? I've tried all kinds of examples with no success.我尝试了各种示例,但均未成功。

Basically what I need is to determine if a user is in a particular dynamic distribution list, I have the distribution list attributes and can search on anything, I'm just having trouble cobbling together the actual query.基本上我需要的是确定用户是否在特定的动态分发列表中,我有分发列表属性并且可以搜索任何内容,我只是在拼凑实际查询时遇到了麻烦。

A一个

the filter or DN will be the out put of过滤器或 DN 将是

GetDynamicDistributionLists

which I'm publishing using an object called我正在使用一个名为的对象发布

DistributionList

and it will look something like this它看起来像这样

(&(extensionAttribute3=xxx)(mailNickname=*)(!(name=SystemMailbox{*))(!(name=CAS_{*))(!(msExchRecipientTypeDetails=16777216))(!(msExchRecipientTypeDetails=536870912))(!(msExchRecipientTypeDetails=8388608)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM