使用C#需要当前组织中的所有用户详细信息(姓名,电子邮件,指定,部门) [英] Need all users detail (Name, Email, Designation, Department) in the current organisation using C#
本文介绍了使用C#需要当前组织中的所有用户详细信息(姓名,电子邮件,指定,部门)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我关注了此链接 C#LDAP查询以检索以下位置的所有用户组织单位 和 引荐已从服务器返回"从C#
I have followed this link C# LDAP query to retrieve all users in an organisational unit and "A referral was returned from the server" exception when accessing AD from C#
我需要知道我在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#在当前组织中的所有用户详细信息(姓名,电子邮件,指定,部门),并在下拉列表中显示这些用户. 请帮忙.
I Need all users detail (Name, Email, Designation, Department) in the current organisation using C# and display those in a dropdownlist. Please help.
推荐答案
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;
}
这篇关于使用C#需要当前组织中的所有用户详细信息(姓名,电子邮件,指定,部门)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文