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 new user to AD

Status
Not open for further replies.

mikemardon

Programmer
May 23, 2005
71
GB
I am trying to implement an 'add new user to active directory' feature into my intranet site.

I found the example below on this forum but some of the references cannot be recognised, I am thinking that this code may not be asp.net VB compatible?

Does anybody (please!) have code I could use instead if this is not going to work?

Thanks


Dim bolCreate As Boolean = True
Dim adPath As String = "LDAP://128.0.100.3"
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=" & lbUserLogonName.Text, "user")

With User
.Properties("sAMAccountName").Value = lbUserLogonName.Text
.Properties("userPrincipalName").Value = tbEmailAddress.Text
.Properties("sn").Value = tbLastName.Text
.Properties("givenName").Value = tbFirstName.Text
.Properties("displayName").Value = Trim(tbLastName.Text & " " & tbFirstName.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 = ddOfficeList.SelectedValue
.Properties("company").Value = "My Company"

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

If tbTelephoneNumber.Text <> "" Then
.Properties("telephoneNumber").Value = tbTelephoneNumber.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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top