AD PropertiesToLoad
以下方式若想改寫成抓取全部 , 想請教一些疑問
撈出ActiveDirectory聯絡人
按下button1 撈出單個使用者:
private void button1_Click(object sender, EventArgs e)
{
string uid = "GGYY"; //Domain帳號
string pwd = "12345"; //密碼
string domain = GetDomainName(domain_txt.Text); //取得Domain
string QueryString = "LDAP://" + domain;
DirectoryEntry ldapConnection = new DirectoryEntry(QueryString, uid, pwd); //使用DirectoryEntry 連入ActiveDirectory
DirectorySearcher Searcher = new DirectorySearcher(ldapConnection);
string value = string.Empty;
string target = target_txt.Text;
try
{
Searcher.Filter = "(SAMAccountName=" + target + ")"; //過濾條件 *有點搞不懂過濾的條件寫法該如何解釋,不知哪位大大能夠幫忙解釋
Searcher.PropertiesToLoad.Add("SAMAccountName"); //帳號
Searcher.PropertiesToLoad.Add("Name"); // 全名
Searcher.PropertiesToLoad.Add("displayName"); //ActiveDirectory的顯示名稱
Searcher.PropertiesToLoad.Add("mail"); //ActiveDirectory的信箱
Searcher.PropertiesToLoad.Add("description");
Searcher.PropertiesToLoad.Add("department"); //ActiveDirectory的信箱
Searcher.PropertiesToLoad.Add("company");
Searcher.PropertiesToLoad.Add("phsicalDeliveryOfficeName");
Searcher.PropertiesToLoad.Add("userPrincipalName"); //使用者帳號+Domain
Searcher.PropertiesToLoad.Add("telephoneNumber");
Searcher.PropertiesToLoad.Add("givenName"); //first name
Searcher.PropertiesToLoad.Add("sn");
SearchResult result = Searcher.FindOne(); //若抓出全部使用者,應該是寫成這個方式 SearchResultCollection result = Searcher.FindAll( );
if (result != null)
{
foreach (string key in result.Properties.PropertyNames)
{
foreach (Object propValue in result.Properties[key])
{
value += key + " = " + propValue + "\r\n";
textBox1.Text += key + " = " + propValue + "\r\n";
}
}
}
else
{
MessageBox.Show("使用者不存在");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Searcher.Dispose();
ldapConnection.Dispose();
}
}
撈出ActiveDirectory聯絡人
按下button1 撈出單個使用者:
private void button1_Click(object sender, EventArgs e)
{
string uid = "GGYY"; //Domain帳號
string pwd = "12345"; //密碼
string domain = GetDomainName(domain_txt.Text); //取得Domain
string QueryString = "LDAP://" + domain;
DirectoryEntry ldapConnection = new DirectoryEntry(QueryString, uid, pwd); //使用DirectoryEntry 連入ActiveDirectory
DirectorySearcher Searcher = new DirectorySearcher(ldapConnection);
string value = string.Empty;
string target = target_txt.Text;
try
{
Searcher.Filter = "(SAMAccountName=" + target + ")"; //過濾條件 *有點搞不懂過濾的條件寫法該如何解釋,不知哪位大大能夠幫忙解釋
Searcher.PropertiesToLoad.Add("SAMAccountName"); //帳號
Searcher.PropertiesToLoad.Add("Name"); // 全名
Searcher.PropertiesToLoad.Add("displayName"); //ActiveDirectory的顯示名稱
Searcher.PropertiesToLoad.Add("mail"); //ActiveDirectory的信箱
Searcher.PropertiesToLoad.Add("description");
Searcher.PropertiesToLoad.Add("department"); //ActiveDirectory的信箱
Searcher.PropertiesToLoad.Add("company");
Searcher.PropertiesToLoad.Add("phsicalDeliveryOfficeName");
Searcher.PropertiesToLoad.Add("userPrincipalName"); //使用者帳號+Domain
Searcher.PropertiesToLoad.Add("telephoneNumber");
Searcher.PropertiesToLoad.Add("givenName"); //first name
Searcher.PropertiesToLoad.Add("sn");
SearchResult result = Searcher.FindOne(); //若抓出全部使用者,應該是寫成這個方式 SearchResultCollection result = Searcher.FindAll( );
if (result != null)
{
foreach (string key in result.Properties.PropertyNames)
{
foreach (Object propValue in result.Properties[key])
{
value += key + " = " + propValue + "\r\n";
textBox1.Text += key + " = " + propValue + "\r\n";
}
}
}
else
{
MessageBox.Show("使用者不存在");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Searcher.Dispose();
ldapConnection.Dispose();
}
}