我已经按照此链接C#LDAP查询来检索组织单位中的所有用户,并且从C#访问AD时出现“从服务器返回了引用”异常
我需要知道我在LDAP路径中做错了什么吗?
// create your domain context and define what container to search in - here OU=Employees
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MS", "OU=Employees,DC=CompanyName,DC=com");
// define a "query-by-example" principal - here, we search for a UserPrincipal
// that is still active
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Enabled = true;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
我需要使用C#当前组织中的所有用户详细信息(姓名,电子邮件,指定,部门),并将其显示在下拉列表中。请帮忙。
public DataTable FindPersons(string lname, string fname)
{
DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0}*)(sn={1}*))", fname, lname);
SearchResultCollection allResults;
allResults = searcher.FindAll();
DataTable dt = new DataTable();
dt.Columns.Add("DisplayName", typeof(string));
dt.Columns.Add("GivenName", typeof(string));
dt.Columns.Add("SurName", typeof(string));
dt.Columns.Add("MSID", typeof(string));
if (allResults.Count >= 0)
{
for (int i = 0; i < allResults.Count; i++)
{
DirectoryEntry deMembershipUser = allResults[i].GetDirectoryEntry();
deMembershipUser.RefreshCache();
dt.Rows.Add(
(string)deMembershipUser.Properties["displayname"].Value,
(string)deMembershipUser.Properties["givenName"].Value,
(string)deMembershipUser.Properties["sn"].Value,
(string)deMembershipUser.Properties["cn"].Value
);
}
}
return dt;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句