我有一个使用DirectorySearcher的类,它根据以下可选过滤器返回结果:
的samAccountName 给定的名称 SN
但是,我需要创建一个directorysearcher,它从基于AD的soley返回一个用户条目,其值为“extensionAttribute1”。
是否可以使用extensionAttribute1创建仅针对用户帐户搜索AD的过滤器?
这是当前的代码:
public static DataTable tblUserSearchResults(string searchstring, string searchproperty)
{
using (DirectoryEntry de = new DirectoryEntry(ConfigurationManager.ConnectionStrings["ADConnectionString"].ToString()))
{
//create instance of the directory searcher
using (DirectorySearcher deSearch = new DirectorySearcher())
{
//set the search filter
deSearch.SearchRoot = de;
deSearch.SearchScope = SearchScope.Subtree;
switch (searchproperty)
{
case "un":
deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(mail=*)(samAccountName=" + searchstring.Trim() + "*))";
break;
case "fn":
deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(mail=*)(givenname=" + searchstring.Trim() + "*))";
break;
case "ln":
deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(mail=*)(sn=" + searchstring.Trim() + "*))";
break;
default:
deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(mail=*)(sn=" + searchstring.Trim() + "*))";
break;
}
//get the search results
using (SearchResultCollection results = deSearch.FindAll())
{
//Create a new table object
using (DataTable tblResults = new DataTable())
{
tblResults.Columns.Add("UserID");
tblResults.Columns.Add("FirstName");
tblResults.Columns.Add("LastName");
tblResults.Columns.Add("Department");
foreach (SearchResult result in results)
{
using (DirectoryEntry de2 = result.GetDirectoryEntry())
{
if (de2.Properties["samAccountName"].Value != null)
{
DataRow dr = tblResults.NewRow();
dr["UserID"] = de2.Properties["samAccountName"].Value.ToString();
dr["FirstName"] = de2.Properties["givenname"].Value.ToString();
dr["LastName"] = de2.Properties["sn"].Value.ToString();
if (de2.Properties["department"].Value != null)
{
dr["Department"] = de2.Properties["department"].Value.ToString();
}
tblResults.Rows.Add(dr);
}
}
}
tblResults.DefaultView.Sort = "LastName asc, FirstName asc";
return tblResults;
}
}
}
}
}