Problems interacting with LDAP, AD in particular

rhm54

Member
Joined
Apr 9, 2007
Messages
22
Programming Experience
Beginner
I have been tasked with creating a Query and Creation tool that will interact with all three of the directories we have in place. I have been able to create the query portion and I am almost done with the creation portion but I have run into a brick wall.

Early in the development I decided I was going to use a secure connection to the directories but, as I programmed the code I found out the only way I could connect successfully was by using "AuthenticationTypes.ServerBind". When I attempted to use "AuthenticationTypes.Secure" I received a DirectoryServicesCOMException. So I just went with ServerBind.

Now to the problem I am currently having. I can create a user and set attributes but when I attempt to set the password I receive a "invalid target of the invocation message". If anyone knows anything about this I would be in their debt. Even if you can point me to someone elses source code for me to study.

Thanks

Here is the code

VB.NET:
Private Sub CreateADAccount(ByVal strUserName As String, ByVal strPassword As String, ByVal strFirstName As String, ByVal strMiddleName As String, ByVal strLastName As String, ByVal strSSN As String, ByVal strTitle As String)
        Dim dirRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ADServer, My.Settings.strADUserName, My.Settings.strADPassword, AuthenticationTypes.Secure)

Dim NewUser As DirectoryEntry = dirRoot.Children.Add("CN=" & strUserName, "User") 'This is where it will error out if AuthenticationTypes.Secure is used

NewUser.CommitChanges()

        NewUser.Properties("userPrincipalName").Value = strUserName & "@mydomain.com"
        NewUser.Properties("sAMAccountName").Value = strUserName
        NewUser.Properties("givenName").Value = strFirstName
        NewUser.Properties("sn").Value = strLastName
        NewUser.Properties("initials").Value = strMiddleName
        NewUser.Properties("title").Value = strTitle
        NewUser.Properties("employeeNumber").Value = strSSN

        NewUser.CommitChanges()

        Try
            NewUser.Invoke("SetPassword", New Object() {strPassword})

            NewUser.CommitChanges()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
End Sub
 

Latest posts

Back
Top