【发布时间】:2010-12-17 11:50:00
【问题描述】:
我有以下代码使用DirectorySearcher 查询 AD 以获取用户的所有 AD 组。
List<string> Groups = new List<string>();
//initialize the directory entry object
DirectoryEntry dirEntry = new DirectoryEntry(ldapPath);
//directory searcher
DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);
//enter the filter
dirSearcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", username);
//get the member of properties for the search result
dirSearcher.PropertiesToLoad.Add("memberOf");
int propCount;
SearchResult dirSearchResults = dirSearcher.FindOne();
propCount = dirSearchResults.Properties["memberOf"].Count;
string dn;
int equalsIndex;
int commaIndex;
for (int i = 0; i <= propCount - 1; i++)
{
dn = dirSearchResults.Properties["memberOf"][i].ToString();
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (equalsIndex == -1)
{
return null;
}
if (!Groups.Contains(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)))
{
Groups.Add(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
}
}
return Groups;
但是当我检查 AD 中的“memberof”选项卡中的用户时,我还有一个额外的组“域用户”,我没有通过此代码。
有什么想法吗?为什么我在“memberof”集合中没有得到“域用户”?
【问题讨论】:
-
可能基于您正在访问的服务器的配置及其角色/功能,请参阅本页的 memberOf 部分msdn.microsoft.com/en-us/library/ms677943
-
好吧。我从这里了解到 - eggheadcafe.com/software/aspnet/30375857/… memberOf 不会返回用户的主要组。但是我可以获得primaryGroupID,它给出了我需要使用它来获取AD组的组的RID(?)。有什么想法吗?
标签: c# .net search active-directory