过滤用户组的成员

时间:2012-02-13 11:17:44

标签: c# .net filter ldap

我已设法获取用户成员组的列表。我想过滤组,所以我只得到包含“嘿”的组。就像是: GroupHeyYou, GroupHeyThere, GroupYouKnow, GroupWhatThe

并且只返回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;
    }

我如何使用过滤器来做到这一点?

1 个答案:

答案 0 :(得分:3)

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