无法在 Active Directory 中 save/update 属性 值

Cant save/update property value in Active Directory

我正在尝试 save/update Active Directory 中的 属性 Surname。它适用于 UserPrincipal class,但我想使用 DirectoryEntry

DirectoryEntry 保存也有效,但不适用于姓氏。不知何故我总是得到例外:

The directory service attribute or value of the specified directory service is not available.

代码:

// This part works fine
var principalUser = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain),IdentityType.SamAccountName, "FirstName.LastName");
principalUser.Surname = "LastName";
principalUser.Save();

// Works not with surname
DirectoryEntry userEntry = (DirectoryEntry)principalUser.GetUnderlyingObject();
userEntry.Properties["surname"].Value = "LastName";
userEntry.CommitChanges(); // --> Exception been thrown here

Microsoft 在 UserPrincipal class 中保存/更新值时有何不同?

我尝试刷新缓存,但它对我不起作用:

userEntry.RefreshCache(new string[] { "surname" });

编辑:

感谢 marc_s 我们可以解决它。当您弄乱 LDAP 中的属性时,一定要始终搜索 Ldap-Display-Name。 在我的例子中 https://msdn.microsoft.com/en-us/library/ms679872(v=vs.85).aspx 我没有看到属性 Surname 的 Ldap-Dipslay-Name 是 "sn"

"surname" 的 LDAP 属性名称是 sn - 试试这个:

DirectoryEntry userEntry = (DirectoryEntry)principalUser.GetUnderlyingObject();
userEntry.Properties["sn"].Value = "LastName";
userEntry.CommitChanges(); 

请参阅 Richard Mueller's web site 以获得非常全面的列表和 Excel 包含所有相关 LDAP 属性、它们的名称和其他属性的工作表