Active directory 属性的DirectoryEntry成员返回完整路径

Active directory 属性的DirectoryEntry成员返回完整路径,active-directory,ldap,Active Directory,Ldap,我只需要用户所属组的通用名称 DirectoryEntry user = new DirectoryEntry("LDAP://cn=myuser...."); foreach(string path in user.Properties["memberOf"]) Console.WriteLine(path); 然后memberOf属性包含一组字符串,即组的完整路径。这是有道理的,但这不是我想要的 我很确定,我不应该为这些路径中的每一个创建一个新的DirectoryEntry来获得公

我只需要用户所属组的通用名称

DirectoryEntry user = new DirectoryEntry("LDAP://cn=myuser....");
foreach(string path in user.Properties["memberOf"])
    Console.WriteLine(path);
然后memberOf属性包含一组字符串,即组的完整路径。这是有道理的,但这不是我想要的

我很确定,我不应该为这些路径中的每一个创建一个新的DirectoryEntry来获得公共名称,但是简单地从路径中解析出cn是最好的方法吗?(这似乎相当残忍)

必须有更好的方法来获取用户所属组的搜索结果

DirectoryEntry user = new DirectoryEntry("LDAP://cn=myuser....");
foreach(string path in user.Properties["memberOf"])
    Console.WriteLine(path);

顺便说一句,这是.NET 2,所以我不能做任何花哨的LINQ to AD的东西,也不能访问DirectoryServices for ActiveDirectory中的新位。

不幸的是,在.NET 2.0中没有比您描述的更好的方法了。
memberOf
属性只包含用户所属的所有组的完整可分辨名称,因此最好的解决方案是解析每个可分辨名称。

CN不一定等于组的名称。不建议从DN中解析它,因为DN是转义的。您需要查询目录中的对象

要检索单个对象,请将搜索基础设置为其可分辨名称,将搜索范围设置为“基础”,然后发出查询

建议在应用程序中缓存查询结果,以避免多次发出相同的LDAP查询(如果您检索一行中多个对象的
memberOf

示例代码(,仅稍作修改):


在“相关”部分找到此旧线程

对这个问题还有两条建议。
它们中的每一个都可以在一次搜索中以
SearchResult
的形式直接获取memberOf属性中的对象

所有代码都是C语言

属性范围查询(ASQ):

DirectoryEntry userEntry = new DirectoryEntry("LDAP://<server>/<user DN>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(userEntry);
searcher.SearchScope = SearchScope.Base;
searcher.AttributeScopeQuery = "memberOf";
searcher.PropertiesToLoad.Clear();
// just load any attributes you want, not limited to cn
searcher.PropertiesToLoad.Add("cn");

foreach (SearchResult result in searcher.FindAll())
{
    Console.WriteLine(result.Path);
}
DirectoryEntry rootEntry = new DirectoryEntry("GC://<GC server>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(rootEntry);
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(member:1.2.840.113556.1.4.1941:=<user DN>)";
searcher.PropertiesToLoad.Clear();
// just load any attributes you want, not limited to cn
searcher.PropertiesToLoad.Add("cn");

foreach (SearchResult result in searcher.FindAll())
{
    Console.WriteLine(result.Path);
}
DirectoryEntry userEntry=新的DirectoryEntry(“LDAP://”、“用户”、“pwd”);
DirectorySearcher search=新的DirectorySearcher(userEntry);
searcher.SearchScope=SearchScope.Base;
searcher.AttributeScopeQuery=“memberOf”;
searcher.PropertiesToLoad.Clear();
//只需加载您想要的任何属性,而不限于cn
searcher.PropertiesToLoad.Add(“cn”);
foreach(searcher.FindAll()中的SearchResult)
{
Console.WriteLine(result.Path);
}
限制:

  • 不处理主要组成员资格
  • 需要2003年的功能级别(忘记域/林)
  • ASQ不能跨域工作(至少System.DirectoryServices不能,它将为另一个域中的任何对象引发异常)
LDAP\u匹配规则\u链中匹配规则:

DirectoryEntry userEntry = new DirectoryEntry("LDAP://<server>/<user DN>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(userEntry);
searcher.SearchScope = SearchScope.Base;
searcher.AttributeScopeQuery = "memberOf";
searcher.PropertiesToLoad.Clear();
// just load any attributes you want, not limited to cn
searcher.PropertiesToLoad.Add("cn");

foreach (SearchResult result in searcher.FindAll())
{
    Console.WriteLine(result.Path);
}
DirectoryEntry rootEntry = new DirectoryEntry("GC://<GC server>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(rootEntry);
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(member:1.2.840.113556.1.4.1941:=<user DN>)";
searcher.PropertiesToLoad.Clear();
// just load any attributes you want, not limited to cn
searcher.PropertiesToLoad.Add("cn");

foreach (SearchResult result in searcher.FindAll())
{
    Console.WriteLine(result.Path);
}
DirectoryEntry rootEntry=newdirectoryEntry(“GC://”、“user”、“pwd”);
DirectorySearcher search=新的DirectorySearcher(rootEntry);
searcher.SearchScope=SearchScope.Subtree;
searcher.Filter=“(成员:1.2.840.113556.1.4.1941:=)”;
searcher.PropertiesToLoad.Clear();
//只需加载您想要的任何属性,而不限于cn
searcher.PropertiesToLoad.Add(“cn”);
foreach(searcher.FindAll()中的SearchResult)
{
Console.WriteLine(result.Path);
}
限制:

  • 不处理主要组成员资格
  • 要求功能级别为2008 R2(忘记域/林)
  • 它获得嵌套的组成员资格,而不仅仅是一个级别的成员资格