mikemardon
Programmer
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
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