2014-09-10 103 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 

的事情 - 不仅仅是名称本身。

如果没有什么帮助 - 只是回到老派的调试技术:

  • 更新只是一个单一的财产;如果失败 - >这是你的问题案例 - 找出为什么这是一个问题。
  • 如果它的工作原理:取消第二属性,然后再次运行

- >重复一遍又一遍,直到你找到你的罪魁祸首