2014-09-10 87 views
3

我有在Active Directory數據庫更新用戶信息的問題...更新AD用戶信息

當我運行下面的代碼我得到這個錯誤:

The specified directory service attribute or value does not exist

的問題是路徑它使用保存的信息是這樣的:

CN=AD Test,OU=Container Name,DC=us,DC=flg,DC=int 

Ad Test是AD,我試圖更新的用戶名。

,我相信這應該是:

CN=Ad Test,OU=Container Name, OU=Server Name,DC=us,DC=flg,DC=int 

我是新來的目錄服務,所以我將不勝感激在找出爲什麼我不能更新任何幫助......謝謝你在前進

public bool UpdateActiveDirectory(string LdapServerName, string CustId, Employee SQLresult) 
{ 
    try 
    { 
     DirectoryEntry rootEntry = new DirectoryEntry("LDAP://" + LdapServerName, "usrename", "password", AuthenticationTypes.Secure); 

     DirectorySearcher searcher = new DirectorySearcher(rootEntry); 
     searcher.Filter = "(sAMAccountName=" + SQLresult.LogonNT + ")"; 
     searcher.PropertiesToLoad.Add("title"); 
     searcher.PropertiesToLoad.Add("street"); 
     searcher.PropertiesToLoad.Add("1"); 
     searcher.PropertiesToLoad.Add("st"); 
     searcher.PropertiesToLoad.Add("postalCode"); 
     searcher.PropertiesToLoad.Add("department"); 
     searcher.PropertiesToLoad.Add("mail"); 
     searcher.PropertiesToLoad.Add("manager"); 
     searcher.PropertiesToLoad.Add("telephoneNumber"); 

     SearchResult result = searcher.FindOne(); 

     if (result != null) 
     { 
      // create new object from search result  
      DirectoryEntry entryToUpdate = result.GetDirectoryEntry(); 

      entryToUpdate.Properties["title"].Value = SQLresult.Title; 
      entryToUpdate.Properties["street"].Value = SQLresult.Address; 
      entryToUpdate.Properties["1"].Value = SQLresult.City; 
      entryToUpdate.Properties["st"].Value = SQLresult.State; 
      entryToUpdate.Properties["postalCode"].Value = SQLresult.ZipCode; 
      entryToUpdate.Properties["department"].Value = SQLresult.Department; 
      entryToUpdate.Properties["mail"].Value = SQLresult.EMailID; 
      entryToUpdate.Properties["manager"].Value = SQLresult.ManagerName; 
      entryToUpdate.Properties["telephoneNumber"].Value = SQLresult.Phone; 

      entryToUpdate.CommitChanges(); 

      Console.WriteLine("User Updated"); 
     } 
     else 
     { 
      Console.WriteLine("User not found!"); 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Exception caught:\n\n" + e.ToString()); 
    } 

    return true; 
} 
+0

也許只是一個錯字?你試圖更新的第三個屬性:'entryToUpdate.Properties [「1」]。Value = SQLresult.City;' - 是那裏的一個*('1')嗎?它應該是一個小的'L'('l')。此外:經理的姓名必須是經理的**專有名稱**整個CN =經理CN =廣告測試OU =容器名稱OU =服務器名稱DC = us DC = flg DC = int'的東西 - 不只是名字本身。 – 2014-09-10 16:16:12

+0

如果這沒有任何幫助 - 回到老派調試技術:更新一個**單**屬性 - 如果失敗 - >這是你的問題案例 - 弄清楚*爲什麼這是一個問題。如果有效:取消註釋第二個屬性並再次運行 - >重複一遍又一遍,直到找到罪魁禍首 – 2014-09-10 16:19:25

+0

謝謝marc_s ...解決了這個問題...當我嘗試提交改變它說:「拒絕訪問」。我認爲它會使用我最初通過的憑據。我將如何在提交期間使用它們? – michaelk46 2014-09-10 16:47:20

回答

3

也許只是一個錯字?

你想更新的第三個屬性:

entryToUpdate.Properties["1"].Value = SQLresult.City; 

是一個(1)在那裏?它應該是一個小L(l)。

另外:經理的姓名必須是管理員的專有名稱 - 全

CN=Manager,CN=Ad Test,OU=Container Name, OU=Server Name,DC=us,DC=flg,DC=int 

的事情 - 不僅僅是名稱本身。

如果沒有什麼幫助 - 只是回到老派的調試技術:

  • 更新只是一個單一的財產;如果失敗 - >這是你的問題案例 - 找出爲什麼這是一個問題。
  • 如果它的工作原理:取消第二屬性,然後再次運行

- >重複一遍又一遍,直到你找到你的罪魁禍首