Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Name

Status
Not open for further replies.

HotBob

Programmer
May 26, 2006
37
GB
I am setting up a new user in Active Directory like so

Code:
 Dim bolCreate As Boolean = True

        Dim adPath As String = "LDAP://........."
        Dim adDirectory As New DirectoryEntry(adPath)

        adDirectory.AuthenticationType = AuthenticationTypes.ServerBind
        adDirectory.Username = ConfigurationSettings.AppSettings("LDAPUser")
        adDirectory.Password = ConfigurationSettings.AppSettings("LDAPPassword")


  
        'Add User to folder Staff in Active Directory.
        adSearchUser.Filter = "(&(OU=Staff))"
        adSearchUserResult = adSearchUser.FindOne

        If Not adSearchUserResult Is Nothing Then

            Dim myEntries As DirectoryEntries = adSearchUserResult.GetDirectoryEntry.Children
            Dim User As DirectoryEntry = myEntries.Add("CN=" & Me.txtLoginID.Text, "user")

            With User
                .Properties("sAMAccountName").Value = Me.txtLoginID.Text
                .Properties("userPrincipalName").Value = Me.txtLoginID.Text & "@fad1.fitzhardingeplc.co.uk"
                .Properties("sn").Value = Me.txtSurname.Text
                .Properties("givenName").Value = Me.txtFirstName.Text
                .Properties("displayName").Value = Trim(Me.txtSurname.Text & " " & Me.txtFirstName.Text)

                If Me.txtJob.Text <> "" Then
                    .Properties("title").Value = Me.txtJob.Text
                End If

                .Properties("Department").Value = Me.comboDepartment.SelectedValue
                .Properties("description").Value = Me.comboDepartment.SelectedValue
                .Properties("physicalDeliveryOfficeName").Value = Me.comboOffice.SelectedValue
                .Properties("company").Value = "My Company"

                If Me.txtInitial.Text <> "" Then .Properties("initials").Value = Me.txtInitial.Text

                If Me.txtExtension.Text <> "" Then
                    .Properties("telephoneNumber").Value = ValidatePhone(Me.txtExtension.Text)
                End If

                If Me.txtMobile.Text <> "" Then
                    .Properties("mobile").Value = ValidatePhone(Me.txtMobile.Text)
                End If


                If Me.txtFax.Text <> "" Then
                    .Properties("facsimileTelephoneNumber").Value = ValidatePhone(Me.txtFax.Text)
                End If

                'Address Details
                If strStreet <> "" Then
                    .Properties("streetAddress").Value = strStreet
                End If

                If strCity <> "" Then
                    .Properties("l").Value = strCity
                End If

                If strCounty <> "" Then
                    .Properties("st").Value = strCounty
                End If

                If strPostalCode <> "" Then
                    .Properties("postalCode").Value = strPostalCode
                End If

                'Secretary
                If Me.txtSec.Value <> "" Then
                    .Properties("msExchAssistantName").Value = Me.txtSec.Value
                End If

                If strSecNo <> "" Then
                    .Properties("telephoneAssistant").Value = ValidatePhone(strSecNo)
                End If

                .Properties("userAccountControl").Value = ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT Or ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD 

                Try
                    .CommitChanges()
                    .Invoke("setPassword", New Object() {strPassword})
                    .Properties("pwdLastSet")(0) = 0
                Catch ex As Exception
                    bolCreate = False
                End Try

            End With

      End if

This all works fine. However, when I go into Active Directory under the name column when you view the list of users there is the samAccountName.
Therefore in my code when I set .Properties("name").Value = "First Surname" this doesn't work.

Any ideas how to set the name equal to first & surname instead of sAMAccountName?
 
Hi HotBob

Sorry to bother you but is this code for asp.net vb?

I am trying to do exactly what you have demonstrated above but some of the variables/commands are not recognised!


for instance : adSearchUser.Filter and similar get underlined in my code as well as ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT and ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD

I have looked all over the net but cannot find any asp.net vb examples :(

And advice would be much appreciated!

Thanks again
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top