private string[] GetUserUnit(string domainADsPath, string username, string password, string schemaClassNameToSearch)
{
SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
string[] sRe = GetGetUserUnitResults(results);
results.Dispose();
return sRe;
}
private string[] GetGetUserUnitResults(SearchResultCollection results)
{
string sRe = string.Empty;
if (results.Count == 0)
throw new Exception("域中没有任何用户");
else
{
foreach (SearchResult result in results)
{
string adPath = result.Path;
if (adPath.IndexOf("OU=用户") < 0)
continue;
//“组织名,登录名,用户名,邮件地址”
// 分解Path得到组织名
string[] sSplit = adPath.Split(',');
string orz = string.Empty;
if (sSplit.Length > 2)
{
orz = sSplit[1].Split('=')[1];
}
ResultPropertyCollection propColl = result.Properties;
if (propColl["samaccountname"].Count > 0)
orz += "," + propColl["samaccountname"][0].ToString();
if (propColl["name"].Count > 0)
orz += "," + propColl["name"][0].ToString();
if(propColl["mail"].Count > 0)
orz += "," + propColl["mail"][0].ToString();
sRe += orz + "=";
}
}
if (sRe.Length > 0)
sRe = sRe.Substring(0, sRe.Length - 1);
return sRe.Split('=');
}