检索Active Directory用户 [英] Retrieving Active Directory users
本文介绍了检索Active Directory用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在域中有数千个Active Directory用户.
现在,我想检索一些(例如前100名)Active Directory用户.
怎么做.
问候
Hi,
I have some thousands of Active directory users in the domain.
Now i want to retrieve some( say top 100) Active directory users.
How to do it.
Regards
推荐答案
尝试以下操作:
Try the following :
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
string userNames="Users : ";
foreach (DirectoryEntry child in directoryEntry.Children)
{
if (child.SchemaClassName == "User")
{
userNames += child.Name + Environment.NewLine ;
}
}
MessageBox.Show(userNames);
^ ]
如何获取Active Directory组中的所有用户
公共静态数据集GetUsersForGroup(字符串GroupName)
{
DataSet dsUser =新的DataSet();
DirectoryEntry de = GetDirectoryObject();
//为可怕的搜索者创建实例
DirectorySearcher deSearch = new DirectorySearcher();
//设置搜索过滤器
deSearch.SearchRoot = de;
//deSearch.PropertiesToLoad.Add("cn);
deSearch.Filter =(&(objectClass = group)(cn =" + GroupName +))";
//获取分组结果
SearchResult results = deSearch.FindOne();
//在数据集中创建一个新的表对象
DataTable tbUser = dsUser.Tables.Add("Users");
tbUser.Columns.Add("UserName");
tbUser.Columns.Add("DisplayName");
tbUser.Columns.Add("EMailAddress");
//创建默认行
DataRow rwDefaultUser = tbUser.NewRow();
rwDefaultUser ["UserName"] ="0";
rwDefaultUser ["DisplayName"] =(未指定)";
rwDefaultUser ["EMailAddress"] =(未指定)";
tbUser.Rows.Add(rwDefaultUser);
//如果该组有效,则继续,否则返回空白数据集
if(结果!=空)
{
//创建指向组对象的链接,这样我们就可以获取成员列表
//在组内
DirectoryEntry deGroup =新的DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//分配属性集合
System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
int n = pcoll ["member"].Count;
//如果该组有成员,则获取详细信息并分配给表
for(int l = 0; l< n; l ++)
{
//创建指向用户对象的链接,可以获取FirstName,LastName和SUername
DirectoryEntry deUser =新DirectoryEntry(ADFullPath +"/" + pcoll ["member"] [l] .ToString(),ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//设置一个新的空行
DataRow rwUser = tbUser.NewRow();
//填充列
rwUser ["UserName"] = GetProperty(deUser,"cn");
rwUser ["DisplayName"] = GetProperty(deUser,"givenName")+" + GetProperty(deUser,"sn");
rwUser ["EMailAddress"] = GetProperty(deUser,"mail");
//将该行追加到数据集的表中
tbUser.Rows.Add(rwUser);
//关闭目录条目对象
deUser.Close();
}
de.Close();
deGroup.Close();
}
返回dsUser;
}
How to get all users in an Active Directory Group
public static DataSet GetUsersForGroup(string GroupName)
{
DataSet dsUser = new DataSet();
DirectoryEntry de = GetDirectoryObject();
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();
//set the search filter
deSearch.SearchRoot =de;
//deSearch.PropertiesToLoad.Add("cn");
deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName +"))";
//get the group result
SearchResult results= deSearch.FindOne();
//Create a new table object within the dataset
DataTable tbUser = dsUser.Tables.Add("Users");
tbUser.Columns.Add("UserName");
tbUser.Columns.Add("DisplayName");
tbUser.Columns.Add("EMailAddress");
//Create default row
DataRow rwDefaultUser = tbUser.NewRow();
rwDefaultUser ["UserName"]= "0";
rwDefaultUser ["DisplayName"]="(Not Specified)";
rwDefaultUser ["EMailAddress"]="(Not Specified)";
tbUser.Rows.Add(rwDefaultUser);
//if the group is valid, then continue, otherwise return a blank dataset
if(results !=null)
{
//create a link to the group object, so we can get the list of members
//within the group
DirectoryEntry deGroup= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//assign a property collection
System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
int n = pcoll["member"].Count;
//if there are members fo the group, then get the details and assign to the table
for (int l = 0; l < n ; l++)
{
//create a link to the user object sot hat the FirstName, LastName and SUername can be gotten
DirectoryEntry deUser= new DirectoryEntry(ADFullPath + "/" +pcoll["member"][l].ToString(),ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//set a new empty row
DataRow rwUser = tbUser.NewRow();
//populate the column
rwUser["UserName"]= GetProperty(deUser,"cn");
rwUser["DisplayName"]= GetProperty(deUser,"givenName") + " " + GetProperty(deUser,"sn");
rwUser["EMailAddress"]= GetProperty(deUser,"mail");
//append the row to the table of the dataset
tbUser.Rows.Add(rwUser);
//close the directory entry object
deUser.Close();
}
de.Close();
deGroup.Close();
}
return dsUser;
}
这篇关于检索Active Directory用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文