ADSI and .NET examples of creating AD Distribution Lists Groups and Adding Users to Groups in Active Directory 2003

I’ve not found too many examples of using .NET and ADSI.  I’m writing a .NET application that adds groups(distribution lists) and users to these lists in Active Directory using VS .NET 2003.  Here are few links that I found useful along with a couple of examples that create a group and then adds a user.  I will recommend one book Active Directory Cookbook for Windows Server 2003 and Windows 2000 by Robbie Allen (Author)

Methods in Active Directory by wwwcoder.com (Good examples!)
http://www.wwwcoder.com/main/parentid/272/site/2150/68/default.aspx


AuthenticationTypes Enumeration
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservicesauthenticationtypesclasstopic.asp


ADSI LDAP Provider
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/account_disabled.asp


HOW TO: Add a User to the Local System by Using Directory Services and Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;306271


BUG: Adding Group Memberships By Using the System.DirectoryServices Namespace Can Truncate Group Members
http://support.microsoft.com/?id=818031


    Sub VerifyDL(ByVal strDLName As String, ByVal strDLDesc As String, ByVal strDomain As String, ByVal strAuthID As String, ByVal strAuthPWD As String)
        Try
     ‘Put the ID and password to authenticate to the domain or pass into the sub from main()
     Dim strAuthID as string = ConfigurationSettings.Appsettings(“authID”)
     Dim strAuthPWS as string = ConfigurationSettings.Appsettings(“password”)
            Dim groupName As String = “MyGroup”
            Dim Description As String = “MyGroup Decription”
            Dim ADsContainer As New DirectoryEntry(“LDAP://DC1/OU=Email,DC=Steve,DC=Schofield,DC=com”)


            ADsContainer.AuthenticationType = AuthenticationTypes.Delegation
            ADsContainer.Username = strAuthID
            ADsContainer.Password = strAuthPWD
            Dim newGroup As DirectoryEntry = ADsContainer.Children.Add(“CN=” + groupName, “group”)
            With newGroup
                .Properties(“saMAccountname”).Value = groupName
                .Properties(“groupType”).Value = 2
                .Properties(“Description”).Value = Description
                .CommitChanges()
            End With
        Catch exp As Exception
            ErrorHandler(exp)
        End Try
    End Sub


    Sub AddUserToDL(ByVal strDLName As String, ByVal strNewUser As String, ByVal strDomain As String, ByVal strAuthID As String, ByVal strAuthPWD As String)
        Try
     ‘Put the ID and password to authenticate to the domain or pass into the sub from main()
     Dim strAuthID as string = ConfigurationSettings.Appsettings(“authID”)
     Dim strAuthPWS as string = ConfigurationSettings.Appsettings(“password”)


            Dim oGrp As DirectoryEntry = New DirectoryEntry(“LDAP://DC1/CN=MySecurtyGroup,OU=Email,DC=Steve,DC=Schofield,DC=com”)
            oGrp.AuthenticationType = AuthenticationTypes.Delegation
            oGrp.Username = strAuthID
            oGrp.Password = strAuthPWD


     ‘BUG: Adding Group Memberships By Using the System.DirectoryServices Namespace Can Truncate Group Members
     ‘http://support.microsoft.com/?id=818031

            oGrp.Invoke(“Add”, New Object() {“LDAP://CN=ANewUser,OU=MyUsers,DC=Steve,DC=Schofield,DC=com”)
            oGrp.CommitChanges()
        Catch exp As Exception
            ErrorHandler(exp)
        End Try
    End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s