筛选用户组的成员

本文关键字:成员 用户组 筛选 | 更新日期: 2023-09-27 18:24:12

我已经设法获得了组成员的用户列表。我想过滤组,所以我只得到包含"嘿"的组。类似于:合友集团,GroupHeyThere,GroupYouKnow,GroupWhat

只返回GroupHeyYou和GroupHeyThere

这是我的功能:

public List<string> GetUserGroupMemberShip()
    {
        DirectoryEntry de = default(DirectoryEntry);            //Binding object.
        DirectorySearcher ds = default(DirectorySearcher);      //Search object.
        SearchResult sr = default(SearchResult);
        List<string> groups = new List<string>();
        string logonUserName = Environment.UserName;
        string logonServer = (System.Environment.GetEnvironmentVariable("logonserver")).Remove(0, 2);
        string activeDirectoryPath = "LDAP://" + logonServer + "." + System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
        try
        {
            de = new DirectoryEntry(activeDirectoryPath);
            ds = new DirectorySearcher(de, "(sAMAccountName=" + logonUserName + ")");
            sr = ds.FindOne();
            if (null != sr)
            {
                DirectoryEntry deUser = new DirectoryEntry(sr.Path);
                object obGroups = deUser.Invoke("Groups");
                foreach (object ob in (IEnumerable)obGroups)
                {
                    DirectoryEntry deGroups = new DirectoryEntry(ob);
                    groups.Add(deGroups.Name);
                }
            }
        }
        catch (Exception)
        {
            return null;
        }
        return groups;
    }

我该如何使用过滤器来完成此操作?

筛选用户组的成员

var filteredGroup = groups.FindAll(item =>
{
    return item.Contains("Hey");
});