I am setting up a new user in Active Directory like so
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?
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?