PermissiveModifyControl throws DirectoryOperationException in C# LDAP

早过忘川 提交于 2020-01-03 18:45:33

问题


Using the System.DirectoryServices.Protocols namespace to add/modify attributes on an Active Directory group. Code:

public void UpdateProperties(Dictionary<string, string> Properties) {

    List<DirectoryAttributeModification> directoryAttributeModifications;

    //  ... Code to convert Properties dictionary to directoryAttributeModifications
    //  There is one 'Add' modification, to set the 'description' of the group

    ModifyRequest modifyRequest = new ModifyRequest(groupDistinguishedName, directoryAttributeModifications.ToArray());
    modifyRequest.Controls.Add(new PermissiveModifyControl());
    ModifyResponse response = connection.SendRequest(modifyRequest) as ModifyResponse;

The PermissiveModifyControl is intended to keep the code from failing if the description already exists. The only information on PermissiveModifyControl I've found is here: http://msdn.microsoft.com/en-us/library/bb332056.aspx

which states:

An LDAP modify request will normally fail if it attempts to add an attribute that already exists or if it attempts to delete an attribute that does not exist. With PermissiveModifyControl the modify operation succeeds without throwing a DirectoryOperationException error.

However, when the above code gets to the SendRequest(), it throws a DirectoryOperationException: "The attribute exists or the value has been assigned."

What I'm trying to avoid is having to query every property in the collection being passed; if it exists, create a Replace DirectoryAttributeModification; if it doesn't, create an Add instead. From what I can glean, PermissiveModifyControl is supposed to do just that.

Can anyone shed some light on why PermissiveModifyControl still throws a DirectoryOperationException, and how to properly use it?

Thanks in advance! James


回答1:


After some more experimenting, I've found that the documentation is misleading... you don't want to add an attribute, you want to replace it (DirectoryAttributeOperation.Replace). If the attribute exists, it will of course replace it. If the attribute does not exist, it will create it.

The rest of my code is correct.



来源:https://stackoverflow.com/questions/3450732/permissivemodifycontrol-throws-directoryoperationexception-in-c-sharp-ldap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!