Modify Attributes

DirContextinterface包含用于修改目录中对象的属性和属性值的方法。

使用修改列表

修改对象属性的一种方法是提供修改请求列表( ModificationItem)。每个ModificationItem由指示要进行的修改类型的数字常量和描述要进行的修改的Attribute组成。以下是三种类型的修改:

修改按照它们在列表中出现的 Sequences 进行应用。要么全部执行修改,要么都不执行。

以下代码创建了一个修改列表。它将"mail"属性的值替换为"geisel@wizards\.com"的值,向"telephonenumber"属性添加了一个附加值,并删除了"jpegphoto"属性。

// Specify the changes to make
ModificationItem[] mods = new ModificationItem[3];

// Replace the "mail" attribute with a new value
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
    new BasicAttribute("mail", "[email protected]"));

// Add an additional value to "telephonenumber"
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
    new BasicAttribute("telephonenumber", "+1 555 555 5555"));

// Remove the "jpegphoto" attribute
mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
    new BasicAttribute("jpegphoto"));

Windows Active Directory: Active Directory 将“电话 Numbers”定义为单值属性,与RFC 2256相反。要使此示例适用于 Active Directory,必须使用“电话 Numbers”以外的属性,或将DirContext\.ADD_ATTRIBUTE更改为DirContext\.REPLACE_ATTRIBUTE


创建此修改列表后,可以按如下所示将其提供给modifyAttributes()

// Perform the requested modifications on the named object
ctx.modifyAttributes(name, mods);

Using Attributes

或者,您可以通过指定修改的类型和应用修改的属性来执行修改。

例如,以下行用orig中的属性替换与name关联的属性(在orig中标识):

ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

name的任何其他属性保持不变。

示例程序演示了modifyAttributes\(\)的这两种用法。该程序通过使用修改列表来修改属性,然后使用modifyAttributes\(\)的第二种形式来还原原始属性。