using System;
using System.Collections.Generic;
using System.Text;
namespace CLBADQuery
{
//
// Active directoy Query 类库
// 包括AD的查询,浏览,导出,自定义查询等操作.
// 参考资料 : www
// Protocol : LDAP
// Auther : GJG
// Mail : xiaojiong1983@gmail.com
// Web : xiaojiong.xinwen365.net
// Date : 2006-8-28
//
public class ADQuery
{
public string m_strLastError = "";
private string m_strValueSpilter = "-";
private string m_strNoneValueSign = "none";
public ADQuery()
{
m_strLastError = "No error";
}
public ADQuery(char szValueSpilter, string strNoneValueSign)
{
m_strLastError = "No error";
m_strValueSpilter = szValueSpilter.ToString();
m_strNoneValueSign = strNoneValueSign;
}
public string GetLastError()
{
return m_strLastError;
}
public string ExtractUserName(string path)
{
string[] userPath = path.Split(new char[] { '//' });
if (0 >= userPath.Length)
{
return "";
}
else
{
return userPath[userPath.Length - 1];
}
}
public string ExtractOUName(string path)
{
string[] userPath = path.Split(new char[] { '=' });
if (0 >= userPath.Length)
{
return "";
}
else
{
return userPath[userPath.Length - 1];
}
}
public string ExtractGroupName(string path)
{
string[] userPath = path.Split(new char[] { '=' });
string[] strGroup = userPath[1].Split(new char[] { ',' });
if (0 > strGroup.Length)
{
return "";
}
else
{
return strGroup[0];
}
}
public static bool UserExists(System.DirectoryServices.DirectoryEntry de,
string UserName)
{
//create instance fo the direcory searcher
System.DirectoryServices.DirectorySearcher deSearch = new System.DirectoryServices.DirectorySearcher();
//set the search filter
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
//find the first instance
System.DirectoryServices.SearchResultCollection results = deSearch.FindAll();
//if the username and password do match, then this implies a valid login
//if so then return the DirectoryEntry object
if (results.Count == 0)
{
return false;
}
else
{
return true;
}
}
//
// 登陆AD
//
public System.DirectoryServices.DirectoryEntry LogonDC(string strPath,
string strUserName, string strPsw)
{
System.DirectoryServices.DirectoryEntry deEntry =
new System.DirectoryServices.DirectoryEntry();
deEntry.Path = "LDAP://" + strPath;
deEntry.Password = strPsw;
deEntry.Username = strUserName;
return deEntry;
}
//
// 获得活动目录当前目录下的Group
//
public System.Collections.ArrayList GetAllGroupList(string strPath,
string strName, string strPsw)
{
System.Collections.ArrayList alGroupList =
new System.Collections.ArrayList();
System.DirectoryServices.DirectoryEntry deEntry =
new System.DirectoryServices.DirectoryEntry();
deEntry.Path = strPath;
deEntry.Username = strName;
deEntry.Password = strPsw;
System.DirectoryServices.DirectorySearcher search =
new System.DirectoryServices.DirectorySearcher();
// search.Filter = "!(objectClass=user)";
// search.Filter = "(objectCategory=group)";
search.SearchRoot = deEntry;
search.PropertiesToLoad.Add("CN");
search.SearchScope = System.DirectoryServices.SearchScope.OneLevel;
try
{
System.DirectoryServices.SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
if (resultCol.Count > 0)
{
foreach (System.DirectoryServices.SearchResult Result in resultCol)
{
string strDetailInfo = "";
if (strPath == Result.Path)
{
continue;
}
if (Result.Properties.Contains("cn"))
{
strDetailInfo += Result.Properties["cn"][0].ToString();
if (UserExists(deEntry, strDetailInfo))
{
continue;
}
alGroupList.Add((strDetailInfo));
}
}
}
}
}
catch (Exception ex)
{
m_strLastError = ex.Message;
}
finally
{
deEntry.Close();
}
return alGroupList;
}
//
// 获得活动目录下的OU
//
public System.Collections.ArrayList GetAllOUList(string strPath, string strUserName, string strPsw)
{
System.Collections.ArrayList alOUList = new System.Collections.ArrayList();
System.DirectoryServices.DirectoryEntry deEntry = new System.DirectoryServices.DirectoryEntry();
string strOUName;
string strDCName;
deEntry.Path = strPath;
deEntry.Username = strUserName;
deEntry.Password = strPsw;
try
{
foreach (System.DirectoryServices.DirectoryEntry deOU in deEntry.Children)
{
strDCName = deOU.SchemaClassName.ToString();
if (strDCName == "organizationalUnit")
{
strOUName = deOU.Name.ToString();
alOUList.Add(ExtractOUName(strOUName));
}
deOU.Close();
}
}
catch (Exception ex)
{
alOUList.Add(ex.Message);
}
finally
{
deEntry.Close();
}
return alOUList;
}
//
// 搜索AD详细信息
// 返回格式
// [0]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
// [1]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
// ................................
// ................................
// ................................
// ................................
// ................................
// ................................
// ................................
// [n]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
//
public System.Collections.ArrayList GetGroupUserDetail(string strPath, string strUserName,
string strPsw, string strGroupName)
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.DirectoryServices.DirectoryEntry deEntry =
new System.DirectoryServices.DirectoryEntry();
deEntry.Path = strPath;
deEntry.Password = strPsw;
deEntry.Username = strUserName;
string strDetailInfo = "";
System.DirectoryServices.DirectorySearcher search =
new System.DirectoryServices.DirectorySearcher(deEntry);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("memberOf");
search.PropertiesToLoad.Add("SAMAccountName");
search.SearchScope = System.DirectoryServices.SearchScope.OneLevel;
System.DirectoryServices.SearchResult result;
System.DirectoryServices.SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
//
// 搜索AD详细信息
// 返回格式
// [0]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
// [1]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
// ................................
// ................................
// ................................
// ................................
// ................................
// ................................
// ................................
// [n]帐号-姓名-部门-职务-电子邮件-联系电话-属组-扩展属性
//
public System.Collections.ArrayList GetOUUserDetailInfo(string strPath, string strUserName,
string strPsw)
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.DirectoryServices.DirectoryEntry deEntry = new System.DirectoryServices.DirectoryEntry();
deEntry.Path = strPath;
deEntry.Password = strPsw;
deEntry.Username = strUserName;
string strDetailInfo = "";
System.DirectoryServices.DirectorySearcher search =
new System.DirectoryServices.DirectorySearcher(deEntry);
search.Filter = "(objectClass=user)";
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("memberOf");
search.PropertiesToLoad.Add("SAMAccountName");
search.PropertiesToLoad.Add("employeeID");
search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
System.DirectoryServices.SearchResult result;
System.DirectoryServices.SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
private System.DirectoryServices.SearchResultCollection m_resultCol = null;
//
// 获得所有的用户信息
//
public long GetAllUser(System.DirectoryServices.DirectoryEntry searchRoot)
{
long nUserCount = 0;
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(searchRoot);
//search.Filter = "(&(objectCategory=user)(l=*)(company=*)(department=*)(cn=*)(mail=*)(telephoneNumber=*)(employeeID=*))";
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("company");
search.PropertiesToLoad.Add("streetAddress");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("memberOf");
search.PropertiesToLoad.Add("SAMAccountName");
search.PropertiesToLoad.Add("employeeID");
search.PropertiesToLoad.Add("city");
search.PropertiesToLoad.Add("l");
try
{
m_resultCol = search.FindAll();
nUserCount = m_resultCol.Count;
}
catch (Exception ex)
{
m_strLastError = ex.Message;
}
return nUserCount;
}
//
// 判断某一属性的值是否为指定的值
// ret: true fro exsit
//
private bool TrueIsExsitInRes(System.DirectoryServices.SearchResult result, string strFieldName, string strVal)
{
if (null == result || 0 == strVal.Length)
{
return false;
}
try
{
if (result.Properties.Contains(strFieldName) &&
strVal == (String)result.Properties[strFieldName][0])
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
m_strLastError = ex.Message;
return false;
}
finally
{
}
}
//
// 获得所有城市
//
public System.Collections.ArrayList GetAllCity()
{
System.Collections.ArrayList alAllCity = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (result.Properties.Contains("l"))
{
alAllCity.Add((String)result.Properties["l"][0]);
}
}
}
return alAllCity;
}
//
// 获得所有城市下的公司
//
public System.Collections.ArrayList GetAllCompany(string strCity)
{
System.Collections.ArrayList alAllCompany = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (!TrueIsExsitInRes(result, "l", strCity))
{
continue;
}
if (result.Properties.Contains("company"))
{
alAllCompany.Add((String)result.Properties["company"][0]);
}
}
}
return alAllCompany;
}
//
// 获得所有城市下的公司下的部门
//
public System.Collections.ArrayList GetAllDepart(string strCity, string strCompany)
{
System.Collections.ArrayList alAllDepart = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (!TrueIsExsitInRes(result, "l", strCity))
{
continue;
}
if (!TrueIsExsitInRes(result, "company", strCompany))
{
continue;
}
if (result.Properties.Contains("department"))
{
alAllDepart.Add((String)result.Properties["department"][0]);
}
}
}
return alAllDepart;
}
//
// 获得属性值
//
private string GetADFieldValue(System.DirectoryServices.SearchResult result, string strFieldName)
{
if (null == result || 0 == strFieldName.Length)
{
return m_strNoneValueSign;
}
try
{
if (result.Properties.Contains(strFieldName))
{
return result.Properties[strFieldName][0].ToString();
}
else
{
return m_strNoneValueSign;
}
}
catch (Exception ex)
{
m_strLastError = ex.Message;
return m_strNoneValueSign;
}
}
//
// 获得所有城市下的公司下的部门的用户
//
public System.Collections.ArrayList GetAllDepartUser(string strCity, string strCompany, string strDepart)
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.Collections.ArrayList alAllDepartUser = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (!TrueIsExsitInRes(result, "l", strCity))
{
continue;
}
if (!TrueIsExsitInRes(result, "company", strCompany))
{
continue;
}
if (!TrueIsExsitInRes(result, "department", strDepart))
{
continue;
}
string strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
//
// 获得所有城市下的公司下的用户
//
public System.Collections.ArrayList GetAllCompanyUser(string strCity, string strCompany)
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.Collections.ArrayList alAllDepartUser = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (!TrueIsExsitInRes(result, "l", strCity))
{
continue;
}
if (!TrueIsExsitInRes(result, "company", strCompany))
{
continue;
}
string strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
//
// 获得所有城市下的用户
//
public System.Collections.ArrayList GetAllCityUser(string strCity)
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.Collections.ArrayList alAllDepartUser = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
if (!TrueIsExsitInRes(result, "l", strCity))
{
continue;
}
string strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
//
// 获得所有下的用户
//
public System.Collections.ArrayList GetAllUser()
{
System.Collections.ArrayList alDetail = new System.Collections.ArrayList();
System.Collections.ArrayList alAllDepartUser = new System.Collections.ArrayList();
System.DirectoryServices.SearchResult result;
if (m_resultCol != null)
{
for (int counter = 0; counter < m_resultCol.Count; counter++)
{
result = m_resultCol[counter];
string strDetailInfo = "";
//
// 帐号
//
strDetailInfo += GetADFieldValue(result, "SAMAccountName") + m_strValueSpilter;
//
// 姓名
//
strDetailInfo += GetADFieldValue(result, "name") + m_strValueSpilter;
//
// 部门
//
strDetailInfo += GetADFieldValue(result, "department") + m_strValueSpilter;
//
// 职务
//
strDetailInfo += GetADFieldValue(result, "title") + m_strValueSpilter;
//
// 电子邮件
//
strDetailInfo += GetADFieldValue(result, "mail") + m_strValueSpilter;
//
// 联系电话
//
strDetailInfo += GetADFieldValue(result, "telephoneNumber") + m_strValueSpilter;
//
// 属组
//
strDetailInfo += GetADFieldValue(result, "memberOf") + m_strValueSpilter;
//
// 工号
//
strDetailInfo += GetADFieldValue(result, "employeeID");
alDetail.Add(strDetailInfo);
}
}
return alDetail;
}
}
}