【发布时间】:2012-02-24 07:37:19
【问题描述】:
我已经设法获得了用户组成员的列表。我想过滤组,所以我只得到包含“嘿”的组。就像是: 组嘿你, 组嘿那里, GroupYouKnow, 组什么
并且只返回 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;
}
如何使用过滤器来做到这一点?
【问题讨论】:
-
顺便说一句,但你真的应该在完成后清理你的 directoryEntry 和 DirectorySearchers。它们在使用非托管资源时都实现了 IDisposable。
-
这是我第一次使用 directoryEntry og DirectorySearcher 所以不知道我有太多 xD 但谢谢你提到它:)