我只需要用户所属的组的commonName.
DirectoryEntry user = new DirectoryEntry("LDAP://cn=myuser....");
foreach(string path in user.Properties["memberOf"])
Console.WriteLine(path);
然后memberOf属性包含一组字符串,即组的完整路径.这是有道理的,但这不是我想要的.
我很确定我不会为每个路径创建一个DirectoryEntry以获得通用名称,但是从路径中简单地解析cn是最好的主意吗? (这似乎相当野蛮)
必须有更好的方法来获取用户所属的组的SearchResults.
顺便说一句,这是.NET 2,所以我不能做任何花哨的LINQ到AD的东西,也不能访问DirectoryServices for ActiveDirectory中的新位.
最佳答案 CN不一定等于组的名称.由于DN已被转义,因此不建议将其解析为DN.您需要在目录中查询对象.
要检索单个对象,请将搜索库设置为其可分辨名称,将搜索范围设置为“base”并发出查询.
在您的应用程序中缓存查询结果,以避免多次发出相同的LDAP查询(如果您检索连续多个对象的memberOf).
示例代码(right off the MSDN,仅略微修改):
string dn = "LDAP://CN=Group Name,ON=Groups,DC=fabrikam,DC=com";
// Bind to a specific group.
DirectoryEntry entry = new DirectoryEntry(dn);
// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.SearchScope = SearchScope.Base;
mySearcher.PropertiesToLoad.Add("displayName");
// Use the FindOne method to find the group object.
SearchResult resEnt = mySearcher.FindOne();