I have the following code that allows me to update a user's information in Active Directory:
Dim lcresult As String = ""
Dim lcPath As String
Dim sStep As String
Dim sDisplayName As String
Dim sUserPrincipalName As StringsDisplayName = sFirstname + " " + sLastname
sUserPrincipalName = sEmail.Replace("@", "$") + "@" + lcServer
TryDim connectionPrefix As String = "LDAP://" + lcServer + "/" + lcDomain
Dim entry As DirectoryEntry = New DirectoryEntry(connectionPrefix, lcADUser, lcADPassword)
Dim mySearcher As DirectorySearcher = New DirectorySearcher(entry)
mySearcher.Filter =
"(&(objectCategory=user)(objectClass=user)(objectSID=" + ObjectSidSearch(sObjectSID) + "))"Dim result As SearchResult = mySearcher.FindOne()
If result Is Nothing Then
sMessage = "User was not found"
Return -1
End If
lcPath = result.Path
sStep =
"After search and found user"Dim adUser As DirectoryEntry = New DirectoryEntry(lcPath, lcADUser, lcADPassword)
'adUser.Properties("cn").Value = sDisplayNameadUser.Properties("userPrincipalName").Value = sUserPrincipalName
adUser.Properties("givenName").Value = sFirstname
adUser.Properties("sn").Value = sLastname
adUser.Properties("displayName").Value = sDisplayName
adUser.Properties("mail").Value = sEmail
adUser.CommitChanges()
adUser.Close()
adUser.Dispose()
sStep =
"After updating password"Catch ex As Exception
sMessage =
"ERROR: " + ex.ToString()Return -1
End Try
sMessage = sStep
Return 0
The code listed above works when I have the line 'adUser.Properties("cn").Value = sDisplayName commented out.
When I uncomment the line, I get the following error: The directory service cannot perform the requested operation on the RDN attribute of an object.
I now know this is not the correct way to update the CN field, but I have not been able to find any examples.