我在这里变得疯狂,我非常感谢您的帮助! 简单地说,我想使用DirectoryEntry类从Active Directory获取用户名或任何内容。无法从Directoryentry获取用户
我用userprinciple,它工作得很好,但我需要得到的属性(用户的经理)只在DirectoryEntry中可用。
我的问题是,我看了这么多的在线,我从那里得到的代码,但由于某种原因,它永远不会工作,总是返回空值。这里是一个例子:
public static DirectoryEntry GetUser(string UserName)
{
//create an instance of the DirectoryEntry
DirectoryEntry de = new DirectoryEntry("LDAP://" + "OU=AnotherOU,OU=xx,OU=Testvironments,DC=abc,DC=local");
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.SearchRoot = de;
//set the search filter
deSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))";
//deSearch.SearchScope = SearchScope.Subtree;
//find the first instance
SearchResult results = deSearch.FindOne();
//if found then return, otherwise return Null
if (results != null)
{
//de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//if so then return the DirectoryEntry object
return results.GetDirectoryEntry();
}
else
{
return null;
}
}
我不知道为什么这段代码返回null。
在此先感谢。
这里的域名是用户ID,用户试图登录到机器。它是一个独特的ID。我很抱歉,应该在这里使用了其他的东西... – RL89