【问题标题】:How to update value of a "usercert" property in LDAP AD如何更新 LDAP AD 中“usercert”属性的值
【发布时间】:2020-04-30 01:16:23
【问题描述】:

我有一个要求,我需要更新活动目录中存在的计算机的属性(“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();  
}

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

【问题讨论】:

    标签: active-directory ldap directoryentry directorysearcher


    【解决方案1】:

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

    所以当你这样做时:

    result.GetDirectoryEntry().CommitChanges();
    

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

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

    var resultDe = result.GetDirectoryEntry();
    resultDe.Properties["usercert"]).Value = whatever;
    resuleDe.CommitChanges();
    

    【讨论】:

    • 我已经尝试调用 result.GetDirectoryEntry() 一次并使用该对象更新和提交,但 CommitChanges() 抛出“访问被拒绝”。例外。
    • 这意味着您正在使用的帐户 (LDAPUser) 没有更新该帐户的权限(或者可能只是该帐户上的该属性)。
    • 谢谢格拉布里尔!我要求相关团队检查必要的许可。
    猜你喜欢
    • 2020-12-22
    • 2021-11-04
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多