我在這裏變得瘋狂,我非常感謝您的幫助! 簡單地說,我想使用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