我正在使用此代码获取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;
}
}
返回的属性不包含用户的电子邮件地址,后来我找到了一种检索用户的proxyAddress的方法,这正是我所要查找的,但是问题是我只成功检索了主根目录中的用户,而不是特定的人群。
这是只为根目录检索用户的proxyAddress而不是特定组的代码,如我之前提到的,
public List<string> emails()
{
List<string> userlist = new List<string>();
try
{
DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot)
{
Filter = "(&(proxyAddresses=smtp:*))"
};
directorySearch.PropertiesToLoad.Add("proxyAddresses");
var results = directorySearch.FindAll();
foreach (SearchResult sr in results)
{
foreach (String addr in sr.Properties["proxyAddresses"])
userlist.Add(addr.Split(':')[1]);
}
return userlist;
}
catch (Exception ex)
{
return new List<string>();
}
}
那么,有什么办法可以合并两个代码?
答案 0 :(得分:0)
我找到了一种方法,如下所示:
以下是解释此想法的代码:
DirectoryEntry _directoryEntry = null;
private DirectoryEntry SearchRoot
{
get
{
if (_directoryEntry == null)
{
_directoryEntry = new DirectoryEntry(@"LDAP://" + textBox5.Text, textBox3.Text, textBox4.Text, AuthenticationTypes.Secure);
}
return _directoryEntry;
}
}
private void GetUsers(){
List<string> userlist = new List<string>();
try
{
DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot)
{
Filter = "(&(objectClass=group)(SAMAccountName=" + textBox1.Text + "))"
};
directorySearch.PropertiesToLoad.Add("mail");
var results = directorySearch.FindOne();
if (results != null)
{
DirectoryEntry deGroup = new DirectoryEntry(results.Path, textBox3.Text, textBox4.Text);
System.DirectoryServices.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, textBox3.Text, textBox4.Text);
User userobj = User.GetUser(user);
userobj.EmailAddress = GetUserEmail(userobj.LoginName);
userlist.Add(userobj.EmailAddress);
user.Close();
}
}
//listBox1.DataSource = userlist;
userlist.ForEach(item => textBox2.Text += item);
//var t = userlist.Where(item => !string.IsNullOrEmpty(item.FirstName) || !string.IsNullOrWhiteSpace(item.FirstName)).ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace + ex.Source);
}
}
public string GetUserEmail(string usr)
{
try
{
string uid = textBox3.Text;
string pwd = textBox4.Text;
string emailProxy = "";
string emailMail = "";
using (var context = new PrincipalContext(ContextType.Domain, textBox5.Text, uid, pwd))
{
using (UserPrincipal user = new UserPrincipal(context))
{
user.SamAccountName = usr;
using (var searcher = new PrincipalSearcher(user))
{
var r = searcher.FindAll();
foreach (var result in r)
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
if (de.Properties["proxyAddresses"].Value != null)
{
List<string> tmpAddress = new List<string>();
int i = 0;
while (true)
{
try
{
tmpAddress.Add(de.Properties["proxyAddresses"][i].ToString());
i++;
}
catch { break; }
}
string val = tmpAddress.Where(em => em.Contains("SMTP")).FirstOrDefault();
if (!string.IsNullOrEmpty(val))
emailProxy = val.Split(':')[1];
else emailProxy = "";
}
else emailProxy = "";
if (de.Properties["mail"].Value != null)
emailMail = de.Properties["mail"].Value.ToString();
else emailMail = "";
}
}
}
}
return !string.IsNullOrEmpty(emailProxy) ? emailProxy : (!string.IsNullOrEmpty(emailMail) ? emailMail : "");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace + ex.Source);
return "";
}
}