C0d1ngJammer
C0d1ngJammer

Reputation: 550

Cant save/update property value in Active Directory

I am trying to save/update the property Surname in Active Directory. It works fine with the UserPrincipal class, but I want to use DirectoryEntry.

The DirectoryEntry saving does also work but not with the surname. Somehow I always get the exception:

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

Code:

// 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

What does Microsoft do differently when saving / updating a value in the UserPrincipal class?

I tried to refresh the cache, but it doesn't work for me:

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

edit:

Thanks to marc_s we could solve it. Be shure to always search for the Ldap-Display-Name when you messing with properties in LDAP. In my case https://msdn.microsoft.com/en-us/library/ms679872(v=vs.85).aspx I didnt see that the Ldap-Dipslay-Name of the attribut Surname is "sn"

Upvotes: 1

Views: 2021

Answers (1)

marc_s
marc_s

Reputation: 754678

The LDAP attribute name for "surname" is sn - try this:

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

See Richard Mueller's web site for very comprehensive lists and Excel sheets with all the relevant LDAP attributes, their names and other properties

Upvotes: 1

Related Questions