我正在使用Active Directory在自动填充输入字段中返回用户列表。当用户键入组的名称时,我遇到的问题是它返回数据。理想情况下,我想要的只是显示用户,而不是组。这可能吗?非常感谢你的帮助。提前谢谢。
public List<Client> Search(string name)
{
List<Client> results = new List<Client>();
string domainname = "domain";
string rootQuery = "LDAP://domain.com/DC=domain,DC=com";
name = name + "*";
using (DirectoryEntry root = new DirectoryEntry(rootQuery))
{
domainname = root.Properties["Name"].Value.ToString();
using (DirectorySearcher searcher = new DirectorySearcher(root))
{
//searcher.Filter = "(ObjectClass=" + name + ")"; //searchFilter;
searcher.Filter = "(SAMAccountName=" + name + ")"; //searchFilter;
searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("sAMAccountName");
searcher.PropertiesToLoad.Add("mail");
SearchResultCollection src = searcher.FindAll();
foreach (SearchResult sr in src)
{
Client r = new Client();
r.domain = domainname;
// Iterate through each property name in each SearchResult.
foreach (string propertyKey in sr.Properties.PropertyNames)
{
ResultPropertyValueCollection valueCollection = sr.Properties[propertyKey];
if (propertyKey == "mail")
{
foreach (Object propertyValue in valueCollection)
{
r.email = propertyValue.ToString();
}
}
if (propertyKey == "displayname")
{
foreach (Object propertyValue in valueCollection)
{
r.name = propertyValue.ToString();
}
}
if (propertyKey == "samaccountname")
{
foreach (Object propertyValue in valueCollection)
{
r.loginName = propertyValue.ToString();
}
}
}
if (r.name != null && r.email != null)
{
results.Add(r);
}
}
}
}
return results;
}