C#讀取AD資料範例

原本專案需求要時做AD驗證並修改AD資料

後來需求變更

故把取得AD資料的功能做個紀錄

 try
 {
     DirectoryEntry DE = new DirectoryEntry(@"LDAP://" + domain, UID, PWD);
     DirectorySearcher DS = new DirectorySearcher(DE);
     DS.Filter = filter;
     DS.PropertiesToLoad.AddRange(Properties);
     var result = DS.FindAll();

     if (result != null && result.Count > 0)
     {
         foreach (SearchResult item in result)
         {
             var temp = new User_Information();
             ResultPropertyCollection resultPropColl = item.Properties;
             temp.user_no = domainName + @"\" + resultPropColl["samaccountname"][0].ToString();
             temp.user_name = resultPropColl["name"][0].ToString();

             result_data.Add(temp);
         }
     }
 }
 catch (Exception ex)
 {

 }

 

 

以上內容,若有錯誤

煩請各路高手路過指正

謝謝!

<(_ _)>