我正在使用此代码获取 AD 中特定组中的用户列表
private DirectoryEntry _directoryEntry = null;
private DirectoryEntry SearchRoot
{
get
{
if (_directoryEntry == null)
{
_directoryEntry = new DirectoryEntry(_ldapDomain, _user, PBKDF2Algorithm.Decrypt(_password, "PAssword"), AuthenticationTypes.Secure);
}
return _directoryEntry;
}
}
public List<User> GetUserFromGroup(String groupName)
{
List<User> userlist = new List<User>();
try
{
DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot)
{
Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))"
};
var results = directorySearch.FindOne();
if (results != null)
{
DirectoryEntry deGroup = new DirectoryEntry(results.Path, _user, PBKDF2Algorithm.Decrypt(_password, "PAssword"));
PropertyCollection pColl = deGroup.Properties;
int count = pColl["member"].Count;
for (int i = 0; i < count; i++)
{
string respath = results.Path;
string[] pathnavigate = respath.Split("CN".ToCharArray());
respath = pathnavigate[0];
string objpath = pColl["member"][i].ToString();
string path = respath + objpath;
DirectoryEntry user = new DirectoryEntry(path, _user, PBKDF2Algorithm.Decrypt(_password, "!twcActiveDirectory!"));
User userobj = User.GetUser(user);
userlist.Add(userobj);
user.Close();
}
}
return userlist.Where(item => !string.IsNullOrEmpty(item.FirstName) || !string.IsNullOrWhiteSpace(item.FirstName)).ToList();
}
catch (Exception ex)
{
return userlist;
}
}
返回的属性不包含用户的电子邮件地址,后来我找到了一种检索用户代理地址的方法,这正是我正在寻找的,但问题是我只成功检索了主根中的用户,而不是为一个特定的群体。
那么,有什么方法可以合并两个代码?
慕村9548890
相关分类