我有要求從Active Directory中檢索所有與用戶相關的信息。從Active Directory中檢索用戶管理器的代碼會引發異常
我的代碼檢索用戶的username,useremail, Full Name
,但是當我嘗試檢索管理器的名稱時,代碼會拋出異常。
下面是我的代碼:
DataTable table = new DataTable();
table = dt;
DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + domain);
DirectorySearcher dSearch = new DirectorySearcher(dEntry);
SearchResultCollection sResultcol;
try
{
dSearch.Filter = "(objectCategory=organizationalUnit)";
sResultcol = dSearch.FindAll();
foreach (SearchResult sResult in sResultcol)
{
DirectoryEntry dUserEntry = new DirectoryEntry();
DirectorySearcher dSearchUsers = new DirectorySearcher(dEntry);
SearchResultCollection sUserResults;
dSearchUsers.Filter = "(objectClass=User)";
dSearchUsers.SearchScope = SearchScope.Subtree;
sUserResults = dSearchUsers.FindAll();
foreach (SearchResult sUserResult in sUserResults)
{
DataRow dr = table.NewRow();
string empCode = sResult.Properties["pager"].ToString();
if (empCode.Length != 0)
{
dr["empcode"] = empCode;
string namee = sUserResult.Properties["samaccountname"][0].ToString();
dr["name"] = namee;
string disname = sResult.Properties["distinguishedName"][0].ToString();
dr["ou"] = disname;
string manager = sUserResult.Properties["manager"].Value.ToString();
dr["manager"] = manager;
dt.Rows.Add(dr);
}
}
dUserEntry.Close();
}
return dt;
}
catch (Exception ex)
{
throw new Exception("Error at retrieveUsers() : " + ex.Message.ToString());
}
我得到的異常
指數超出範圍。必須是非負值,並且小於集合的大小 。參數名稱:索引
當我嘗試獲取經理姓名。
根據Active Directory的結構,經理的姓名位於另一個選項卡中。
有沒有人有任何想法從Active Directory中的常規選項卡檢索數據?
請幫幫我。
在此先感謝。
'System.DirectoryServices.ResultPropertyCollection'沒有包含'Properties'的定義,也沒有找到接受類型爲'System.DirectoryServices.ResultPropertyCollection'的第一個參數的擴展方法'Properties'你錯過了使用指令或程序集引用?這是當我使用你建議的代碼時得到的錯誤 –
@MansiDesai:對不起 - 我有一個'.Properties'太多了 - 你能再試一次嗎? –