110,538
社区成员
发帖
与我相关
我的任务
分享
public List<string> GetOuList(ref List<string> searchList)
{
List<string> ouList = new List<string>();
DirectoryEntry objADAM; // Binding object.
DirectoryEntry objGroupEntry; // Group Results.
DirectorySearcher objSearchADAM; // Search object.
SearchResultCollection objSearchResults; // Results collection.
// Get the AD LDS object.
string domainName = GetDomainName();
string strPath = "LDAP://" + domainName;
string strPath2 = "LDAP://" + Environment.UserDomainName;
try
{
objADAM = new DirectoryEntry(strPath, txtUserName.Text.Trim(), txtPassword.Text.Trim());
objADAM.RefreshCache();
}
catch (Exception ex)
{
sb.Append(string.Format("找不到LDAP路径:{0}. 详细原因:{1}.", strPath, ex.Message));
sb.Append("\r\n");
return null;
}
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = "(&(objectClass=organizationalUnit))";//group))";
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception ex)
{
sb.Append(string.Format("下过滤条件错误:{0}. 详细原因:{1}.", "organizationalUnit", ex.Message));
sb.Append("\r\n");
return null;
}
try
{
if (objSearchResults != null && objSearchResults.Count != 0)
{
foreach (SearchResult objResult in objSearchResults)
{
string a = objResult.Path;
searchList.Add(a);
try
{
objGroupEntry = objResult.GetDirectoryEntry();
string gPath = GetCorrectPath(objGroupEntry.Path.ToString(), domainName);
if (gPath != null)
{
ouList.Add(gPath);
}
}
catch (Exception ex)
{
sb.Append(string.Format("找组时出错,详细原因:{1}.", ex.Message));
sb.Append("\r\n");
}
}
}
else
{
return null;
}
}
catch (Exception ex)
{
sb.Append(string.Format("跑循环时出错,详细原因:{1}.", ex.Message));
sb.Append("\r\n");
return null;
}
return ouList;
}
附上测试代码!