从Active Directory检索所有用户? [英] Retrieve all users from Active Directory?
本文介绍了从Active Directory检索所有用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在尝试从活动目录中将所有用户检索到排序列表中,但是由于某种原因,它不会返回所有用户,例如我本人.它在客户的广告上,广告中大约有1500多个用户.
我的代码如下
公共SortedList ReadFromActiveDirectoryUsers()
{
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
字符串xmlADPath = string.Empty;
字符串xmlADUser = string.Empty;
字符串xmlADPw = string.Empty;
字符串xmlFile = Path.Combine(SPUtility.GetGenericSetupPath(@"TEMPLATE \ LAYOUTS \"),"Config.XML");
ReadXMLFile xmlReader =新的ReadXMLFile(xmlFile);
xmlADPath = xmlReader.ReadValue("AdConnection","ADPath",false);
xmlADUser = xmlReader.ReadValue("AdConnection","ADUser",false);
xmlADPw = xmlReader.ReadValue("AdConnection","ADPass",false);
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " XMLADPath =" + xmlADPath);
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " XMLADPass =" + xmlADPw);
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " XMLADUser =" + xmlADUser);
SortedList UsersInGroup = new SortedList();
{
字符串strError =";
尝试
{
SortedList AllADUsers = new SortedList();
SortedList AllADUsersReversed = new SortedList(new InverseComparer());
DirectoryEntry de =新的DirectoryEntry(xmlADPath,xmlADUser,xmlADPw);
de.AuthenticationType = AuthenticationTypes.Delegation;
DirectorySearcher deSearch =新的DirectorySearcher(de);
//deSearch.Filter =" objectCategory = user" ;; //仅显示用户
deSearch.Filter ="(&(objectCategory = user)(sAMAccountName = *))" ;;
//deSearch.Filter =" objectClass = user" ;; //包括PC和组
SearchResultCollection results = null;
尝试
{
结果= deSearch.FindAll();
}
抓住(前例外)
{
strError + = ex.Message;
}
//如果该组有效,则继续,否则返回空白数据集
int errorCounter = 0;
int userCounter = 0;
字符串strGivenName =";
字符串strSurname =";
字符串sam =";
字符串DisplayName =";
字符串Displayname2 =";
bool blnFirstRun = true;
foreach(结果中的SearchResult用户)
{
bool blnAdded = false;
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + 在结果中输入了foreach SearchResult用户");
userCounter + = 1;
//AllADUsers.Add(user.GetDirectoryEntry().Properties ["sAMAccountName"].Value.ToString(),user.GetDirectoryEntry().Properties ["name"].Value.ToString()+"|" ;);
//sam = xmlDomain +" \\" + deUser.Properties ["sAMAccountName"].Value.ToString();
如果(user.GetDirectoryEntry().Properties ["sAMAccountName"].Count> 0)
{
sam = user.GetDirectoryEntry().Properties ["sAMAccountName"].Value.ToString();
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + "输入是否(user.GetDirectoryEntry().Properties [sAMAccountName] .Count> 0))";
如果(user.GetDirectoryEntry().Properties ["givenName"].Count> 0)
{
strGivenName = user.GetDirectoryEntry().Properties ["givenName"].Value.ToString();
}
别的
{
strGivenName ="...";
}
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " strGivenName =" + strGivenName +"));
如果(user.GetDirectoryEntry().Properties ["sn"].Count> 0)
{
strSurname = user.GetDirectoryEntry().Properties ["sn"].Value.ToString();
}
别的
{
strSurname ="...";
}
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " strSurname =" + strSurname +"));
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " sam =" + sam +"));
DisplayName = strGivenName +" " + strSurname;
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + " DisplayName =" + DisplayName);
//AllADUsers.Add(DisplayName + sam,DisplayName);
}
DisplayName = strGivenName +" " + strSurname;
如果(DisplayName ==" ... ...")
{
DisplayName = sam +" ...'';
}
Displayname2 = strGivenName + strSurname;
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + 添加所有用户");
AllADUsers.Add(Displayname2 + sam,DisplayName);
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + 完成添加所有用户");
}
strError + =找到的用户数:" + userCounter;
strError + =发现的错误数:" + errorCounter;
foreach(AllADUsers中的DictionaryEntry条目)
{
AllADUsersReversed.Add(entry.Key,entry.Value);
}
返回AllADUsersReversed;
}
捕获(异常exx)
{
SortedList AllADUsersEmpty = new SortedList();
strError + =异常错误::" + exx.Message;
xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug",ns).Value + "错误:=" + strError);
返回AllADUsersEmpty;
}
}
}
我在做什么错了?
这是我第一次使用AD ...
请帮助
解决方案
有人对此有答案吗?Hi
I am trying to retrieve all the users from active directory into a sorted list, but for some reason it does not return all the users like myself for instance. It's on a client's ad and there is probably about 1500+ users in AD.
My code is as follows
public SortedList ReadFromActiveDirectoryUsers() { XPathNavigator xnMyForm = this.CreateNavigator(); XmlNamespaceManager ns = this.NamespaceManager; string xmlADPath = string.Empty; string xmlADUser = string.Empty; string xmlADPw = string.Empty; string xmlFile = Path.Combine(SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\"), "Config.XML"); ReadXMLFile xmlReader = new ReadXMLFile(xmlFile); xmlADPath = xmlReader.ReadValue("AdConnection", "ADPath", false); xmlADUser = xmlReader.ReadValue("AdConnection", "ADUser", false); xmlADPw = xmlReader.ReadValue("AdConnection", "ADPass", false); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADPath = " + xmlADPath); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADPass = " + xmlADPw); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADUser = " + xmlADUser); SortedList UsersInGroup = new SortedList(); { string strError = ""; try { SortedList AllADUsers = new SortedList(); SortedList AllADUsersReversed = new SortedList(new InverseComparer()); DirectoryEntry de = new DirectoryEntry(xmlADPath, xmlADUser, xmlADPw); de.AuthenticationType = AuthenticationTypes.Delegation; DirectorySearcher deSearch = new DirectorySearcher(de); //deSearch.Filter = "objectCategory=user"; // Show only users deSearch.Filter = "(&(objectCategory=user)(sAMAccountName=*))"; //deSearch.Filter = "objectClass=user"; // Include PC and groups SearchResultCollection results = null; try { results = deSearch.FindAll(); } catch (Exception ex) { strError += ex.Message; } //if the group is valid, then continue, otherwise return a blank dataset int errorCounter = 0; int userCounter = 0; string strGivenName = ""; string strSurname = ""; string sam = ""; string DisplayName = ""; string Displayname2 = ""; bool blnFirstRun = true; foreach (SearchResult user in results) { bool blnAdded = false; xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Entered foreach SearchResult user in results"); userCounter += 1; //AllADUsers.Add(user.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString(), user.GetDirectoryEntry().Properties["name"].Value.ToString() + " | "); //sam = xmlDomain + "\\" + deUser.Properties["sAMAccountName"].Value.ToString(); if (user.GetDirectoryEntry().Properties["sAMAccountName"].Count > 0) { sam = user.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString(); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Entered if (user.GetDirectoryEntry().Properties[sAMAccountName].Count > 0)"); if (user.GetDirectoryEntry().Properties["givenName"].Count > 0) { strGivenName = user.GetDirectoryEntry().Properties["givenName"].Value.ToString(); } else { strGivenName = "..."; } xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " strGivenName = " + strGivenName + " "); if (user.GetDirectoryEntry().Properties["sn"].Count > 0) { strSurname = user.GetDirectoryEntry().Properties["sn"].Value.ToString(); } else { strSurname = "..."; } xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " strSurname = " + strSurname + " "); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " sam = " + sam + " "); DisplayName = strGivenName + " " + strSurname; xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " DisplayName = " + DisplayName); //AllADUsers.Add(DisplayName + sam, DisplayName); } DisplayName = strGivenName + " " + strSurname; if (DisplayName == "... ...") { DisplayName = sam + " ..."; } Displayname2 = strGivenName + strSurname; xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " adding all users "); AllADUsers.Add(Displayname2 + sam, DisplayName); xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Finished adding all users"); } strError += "Number of users found:" + userCounter; strError += "Number of errors found:" + errorCounter; foreach (DictionaryEntry entry in AllADUsers) { AllADUsersReversed.Add(entry.Key, entry.Value); } return AllADUsersReversed; } catch (Exception exx) { SortedList AllADUsersEmpty = new SortedList(); strError += "Exception error::" + exx.Message; xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Error: = " + strError); return AllADUsersEmpty; } } }What am I doing wrong?
This is the first time I am working with AD...
Please help
解决方案Does anyone have an answer on this...??
这篇关于从Active Directory检索所有用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文