使用 LDAP 和一般的 C# 相当新,我进行了多次搜索,但我尝试的大多数修复都无济于事。
我正在从 LDAP 中提取信息。一切正常,除了如果我明确需要哪个数组编号,我只能提取 memberOf 信息。尝试使用foreach
, 或for
语句无济于事。我知道我可能遗漏了一些简单的东西,但我想我应该问一下。
public static String FindOther(String userAccount)
{
DirectoryEntry entry = GetDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(entry);
try
{
search.Filter = "(SAMAccountName=" + account + ")";
search.PropertiesToLoad.Add("distinguishedName");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("CN");
search.PropertiesToLoad.Add("Title");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne();
if (result != null)
{
return
"Results for " + userAccount + "\n" +
" DistinguishedName..: " + result.Properties["distinguishedName"][0].ToString() + "\n" +
" Displayname........: " + result.Properties["displayname"][0].ToString() + "\n" +
" eMail..............: " + result.Properties["mail"][0].ToString() + "\n" +
" Common Name........: " + result.Properties["CN"][0].ToString() + "\n" +
" Title..............: " + result.Properties["Title"][0].ToString() + "\n" +
" Last Name..........: " + result.Properties["sn"][0].ToString() + "\n" +
" First Name.........: " + result.Properties["givenname"][0].ToString() + "\n" +
" Telephone..........: " + result.Properties["telephoneNumber"][0].ToString() + "\n" +
" Member Of..........: " + result.Properties["memberOf"][0].ToString() + "\n" +
" Member Of..........: " + result.Properties["memberOf"][1].ToString() + "\n" +
"End Transmission" + "\n";
}
else
{
return "Object not found... User ID: " + account;
}
}
catch (Exception ex)
{
return "Big Ol Error: " + ex.Message + " User ID: " + account;
}
}
感谢大家提供的任何帮助。