Problem updating Active Directory from code

  • Thread starter Thread starter Robin
  • Start date Start date
R

Robin

When using the following ASP.Net code the error "The directory service
cannot perform the requested operation on the RDN attribute of an object."
is displayed when the commit changes is run. What modifications need to be
made to the code to allow it to update the details?

Dim rootDSE As New DirectoryEntry("LDAP://RootDSE")
Dim namingContext As String =
rootDSE.Properties("defaultNamingContext").Value.ToString()
Dim searchRoot As New DirectoryEntry("LDAP://" & namingContext)
Dim searcher As New DirectorySearcher
searcher.SearchRoot = searchRoot
searcher.Filter =
String.Format("(&(objectCategory=person)(objectClass=user)(samAccountName={0
}))", cUser)
searcher.PropertiesToLoad.AddRange(New String() { _
"name", "givenName", "sn", "initials", "personalTitle",
"extensionAttribute1", _
"department", "title", "description", "info", "roomNumber",
_
"telephoneNumber", "mail", "homePostalAddress",
"extensionAttribute2" _
})

Dim result As SearchResult = searcher.FindOne()

Dim onUser As System.DirectoryServices.DirectoryEntry =
result.GetDirectoryEntry

onUser.Properties("name").Value = NZ(tbPreferredName.Text) ' a text
field

' save details back to AD
onUser.CommitChanges()
 
you need to Bind to the native AdsObject to force authentication.

Dim obj As Object = rootDSE.NativeObject

Dim rootDSE As DirectoryEntry = New DirectoryEntry("LDAP://" & domain,
Usernamewith Domain, password)
'Bind to the native AdsObject to force authentication.
Dim obj As Object = rootDSE.NativeObject
Dim searcher As DirectorySearcher = New
DirectorySearcher(rootDSE )
searcher.Filter = "(SAMAccountName=" & username & ")"
searcher.PropertiesToLoad.AddRange(New String() {"name",
"givenName", "sn", "initials", "personalTitle"})

Dim result As SearchResult = searcher.FindOne()
Dim onUser As System.DirectoryServices.DirectoryEntry =
result.GetDirectoryEntry()
If (result Is Nothing) Then
onUser.Properties("name").Value = NZ(tbPreferredName.Text) '
a
End If
 
Back
Top