PermissiveModifyControl在C#LDAP中抛出DirectoryOperationException

使用System.DirectoryServices.Protocols命名空间在Active Directory组上添加/修改属性。 码:

 public void UpdateProperties(Dictionary Properties) { List 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; 

PermissiveModifyControl旨在防止代码在描述已存在时失败。 我发现PermissiveModifyControl的唯一信息在这里: http : //msdn.microsoft.com/en-us/library/bb332056.aspx

其中说明:

如果LDAP修改请求尝试添加已存在的属性或尝试删除不存在的属性,则它通常会失败。 使用PermissiveModifyControl ,修改操作成功,而不会抛出DirectoryOperationException错误。

但是,当上面的代码到达SendRequest() ,它会抛出DirectoryOperationException :“属性存在或值已分配”。

我想避免的是必须查询正在传递的集合中的每个属性; 如果存在,则创建Replace DirectoryAttributeModification ; 如果没有,请创建一个Add 。 从我可以收集到的, PermissiveModifyControl应该做到这一点。

任何人都可以解释为什么PermissiveModifyControl仍然会抛出DirectoryOperationException ,以及如何正确使用它?

提前致谢! 詹姆士

经过一些实验,我发现文档有误导性…你不想添加属性,你想要替换它( DirectoryAttributeOperation.Replace )。 如果该属性存在,它当然会替换它。 如果该属性不存在,则会创建该属性。

我的其余代码是正确的。