Using System.DirectoryServices to create a new user in Active Directory

Click here for original page

LDAP Naming Structure

As I mentioned in my previous post an LDAP directory (such as Active Directory) stores data hierarchically. How do we identify a specific object in this data tree?

Distinguished Name: The DN uniquely identifies an object in the tree. The DN is very similar to the way a file path in windows explorer uniquely identifies a file on your computer.
Example: cn=John Brennan,OU=Users,DC=DigeratiSoftware,DC=local uniquely identifies the user John Brennan who is stored in the Users container of the DigeratiSoftware.com domain.
Relative Distinguished Names: The RDN uniquely identifies an object among its siblings (i.e. objects that reside at the same level in the tree). It is the distinguished name minus it’s parent entries.
Example: cn=John Brennan
Introduction to System.DirectoryServices

.NET provides the System.DirectoryServices namespace to allow you access the Active Directory from managed code. The namespace is pretty compact and there are really only 2 classes that you need to be aware of in order for you to start coding, DirectoryEntry and DirectorySearcher.

The DirectoryEntry class represents a node in the LDAP tree. You can think of the DirectoryEntry class as being akin to the SqlConnection class in System.Data.SqlClient. We use DirectoryEntry to establish connections to a specific node in the directory. However DirectoryEntry can also be used to inspect the properties of that node, modify them and then save any changes back to the directory.

Note: For these example to work you will need to add a reference to the System.DirectoryServices namespace in your Visual Studio project. Also you will need to import the COM Active DS Type library and add an imports ActiveDS to the top of your code file.

Example: Bind to Active Directory and create a new user


Imports System.DirectoryServices
Imports ActiveDs

Module Module1
    Sub Main()

        ' connect to my Active Directory
        Dim root As New DirectoryEntry("LDAP://MyDomainControllerServer/dc=digeratisoftware,dc=local")

        Try
            ' create a new user object whose RDN is John Brennan
            Dim user As DirectoryEntry = root.Children.Add("CN=John Brennan", "user")


            ' set properties on the user
            user.Properties("givenName").Value = "John"
            user.Properties("sn").Value = "Brennan"
            user.Properties("mail").Value = "john@somemailaddress.com"
            user.Properties("description").Value = "new test user"
            user.Properties("sAMAccountName").Value = "John.Brennan"            

             ' userPrincipalName. This property is domain specific
            user.Properties("description").Value= "John.Brennan@digeratsoftware.local" 

            ' enable the user account and set their password to never expire
            user.Properties("userAccountControl").Value = ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT Or ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD Or ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD

 

            ' commit the object from memory to the directory store
            user.CommitChanges()

            ' next set the user's password
            user.Invoke("SetPassword", New Object() {"mypassword"})

        Catch ex As Exception

            Throw

        End Try

    End Sub

End Module