我有這樣的代碼從一組獲取用戶
DirectorySearcher myGroupSearcher = new DirectorySearcher(myDirectoryEntry);
myGroupSearcher.Filter = String.Format("(&(objectClass=group)(|(cn={0})(dn={0})))", strGroupName);
myGroupSearcher.PropertiesToLoad.Add("member");
SearchResult myGroupSearchResult = myGroupSearcher.FindOne();
if (myGroupSearchResult != null)
{
ResultPropertyValueCollection myUsersInGroup = myGroupSearchResult.Properties["member"];
int intMemberCount = myUsersInGroup.Count;
for (int i = 0; i < intMemberCount; i++)
{
//Split the current result
string[] strProperites = myUsersInGroup[i].ToString().Split(',');
//Get the CN
string strUsername = strProperites[0].Substring(3);
DirectorySearcher myUserSearcher = new DirectorySearcher(myDirectoryEntry);
myUserSearcher.Filter = String.Format("(&(objectClass=user)(|(cn={0})(sAMAccountName={0})))", strUsername);
myUserSearcher.PropertiesToLoad.Add("memberOf");
SearchResult myUserSearchResult = myUserSearcher.FindOne();
//Do some work
}
}
這適用於大多數用戶與用戶的工作,但對於一些人來說,strUserName中得到turncated取決於客戶廣告的樣子(如果用戶有一個CN包含,)。所以這個解決方案不是最優化的。搜索組中的成員時,有沒有辦法獲得samaccount名稱?或者,還有更好的方法?
快速和簡單的解決方案,我的客戶會喜歡它:) – 2011-05-18 09:06:00