0

我有一个要求,我需要更新活动目录中存在的计算机的属性(“usercert”)中保存的值。

// 从 AD 中检索属性值

DirectoryEntry entry = new DirectoryEntry(LDAPPath, LDAPUser, DecryptPwd(LDAPPwd, LDAPKey)); 
DirectorySearcher searcher = new DirectorySearcher(entry); 
searcher.Filter = string.Format("(&(objectCategory=computer)(Name=" + MachineName + "))"); 
result = searcher.FindOne(); 
byte[] text= (byte[])result.GetDirectoryEntry().Properties["usercert"].Value;

// 将新值更新为 AD 字符串 updatedText= "New Text";

if (result.GetDirectoryEntry().Properties["usercert"] != null && 
              result.GetDirectoryEntry().Properties["usercert"].Value != null) 
{
     byte[] updatedTextByte = Encoding.ASCII.GetBytes(updatedText);
     result.GetDirectoryEntry().InvokeSet("usercert", updatedPassByte);
     //(result.GetDirectoryEntry().Properties["usercert"]).Value = Encoding.ASCII.GetBytes(updatedText);
     //result.GetDirectoryEntry().Properties["usercert"].Add(Encoding.ASCII.GetBytes(updatedText));
     //result.GetDirectoryEntry().Properties["usercert"][0] = Encoding.ASCII.GetBytes(updatedText);
     result.GetDirectoryEntry().CommitChanges();  
}

我尝试了所有上述注释代码,但对我没有任何作用。你能帮我解决这个问题吗?

4

1 回答 1

0

每次调用GetDirectoryEntry()都会创建一个新对象,您可以在此处的源代码中看到该对象。DirectoryEntry

所以当你这样做时:

result.GetDirectoryEntry().CommitChanges();

它正在创建一个全新的DirectoryEntry对象并调用CommitChanges()它。所以什么都没有改变。

您只需要调用GetDirectoryEntry()一次并对该对象进行更改。例如:

var resultDe = result.GetDirectoryEntry();
resultDe.Properties["usercert"]).Value = whatever;
resuleDe.CommitChanges();
于 2020-01-14T13:27:29.490 回答