我有此代码,可以从用户,他的电子邮件和部门中检索信息,并向其传递用户名和密码。
string email;
string depa;
private bool AutenticatheUser(String userName, String password)
{
bool ret = false;
try
{
DirectoryEntry de = new DirectoryEntry(GetCurrentDomainPath(), userName, password);
DirectorySearcher dsearch = new DirectorySearcher(de);
dsearch.Filter = "sAMAccountName=" + userName + "";
SearchResult results = null;
results = dsearch.FindOne();
email = results.GetDirectoryEntry().Properties["mail"].Value.ToString();
depa = results.GetDirectoryEntry().Properties["department"].Value.ToString();
ret = true;
}
catch(Exception ex)
{
ret = false;
MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return ret;
}
数据返回示例:
email = juanitoRg@companny.com and depa = US-Oneonta
因此,使用以下代码,我尝试恢复所有US-Oneonta用户,但是在if(depa ==“ US-Oneonta”)之前(如果我拥有部门),我什么也不会恢复。具体来说。
private void GetAllUsers()
{
SearchResult result;
SearchResultCollection results;
DirectorySearcher ds = null;
DirectoryEntry de = new DirectoryEntry(GetCurrentDomainPath());
ds = new DirectorySearcher(de);
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("department");
ds.Filter = "(&(objectCategory=User)(objectClass=person))";
results = ds.FindAll();
int qty = 0;
label1.Text = Convert.ToString(results.Count);
foreach (SearchResult sr in results)
{
if (sr.Properties.Contains("mail") && sr.Properties.Contains("department"))
{
string depa = sr.Properties["department"][0].ToString();
if (depa == "US-Oneonta")
{
int file = dataGridView1.Rows.Add();
dataGridView1.Rows[file].Cells["Column1"].Value = sr.Properties["mail"][0].ToString();
dataGridView1.Rows[file].Cells["Column2"].Value = sr.GetDirectoryEntry().Properties["department"][0].ToString();
}
depa = string.Empty;
}
}
}
如何恢复其部门具有US-Oneonta描述的所有用户?