Write to LDAP with .net 2

neoseason

New member
Joined
Oct 11, 2007
Messages
2
Programming Experience
1-3
Hello
Hope I'm not ot.

I try to write some data to a simple ldap. The directory server is a OpenLdap server. I'm using InetOrgPerson schema. I'm try this one:

VB.NET:
        Dim oIdentifier As nmsLDAP.LdapDirectoryIdentifier = New nmsLDAP.LdapDirectoryIdentifier("test-srv", False, False)
        Dim oConnection As nmsLDAP.LdapConnection = New nmsLDAP.LdapConnection(oIdentifier)

        With oConnection
            .Credential = New System.Net.NetworkCredential("cn=manager,dc=mailenable,dc=local", "password")
            .Timeout = New System.TimeSpan(30000)
            .AutoBind = True
            .AuthType = Protocols.AuthType.Basic
            .SessionOptions.ProtocolVersion = 3
            .SessionOptions.AutoReconnect = True
            .SessionOptions.SecureSocketLayer = False
            .Timeout = New TimeSpan(0, 0, 30)
        End With

and work prety fine. I try it with LdapConnection.bind method.
But now I need to add a new OU then I have try something similar like this:

VB.NET:
        Dim oRequest As nmsLDAP.AddRequest = New nmsLDAP.AddRequest
        Dim ldifOCS As String() = {"organizationalUnit", "top"}     ''object class container
        Dim ldifSTR As String = "prova.it"   ''string container OU
        With oRequest
            .Attributes.Add(New nmsLDAP.DirectoryAttribute("objectClass", ldifOCS))
            .DistinguishedName = ldifSTR
        End With
        ''creo la response della request appena creata
        Dim oResponse As nmsLDAP.AddResponse = DirectCast(oConnection.SendRequest(oRequest), nmsLDAP.AddResponse)

And I have an exception "Distinguished name is wrong or invalid". So I have try to modify ldifSTR in:

VB.NET:
Dim ldifSTR As String = "ou=prova.it"

But have an exception "Object doesn't exist".

Then I have try:

VB.NET:
Dim ldifSTR As String = "ou=prova.it,dc=mailenable,dc=local"

But have another exception...

what's wrong ?!?!
 
Doh... Solved! :D:D:D
Have missed object attributes. The correct code is below (without connection part):

VB.NET:
        ''creo la request per il caricamento della OU
        Dim oRequest As nmsLDAP.AddRequest = New nmsLDAP.AddRequest("dc=mailenable,dc=local")
        Dim ldifOCS As String() = {"organizationalUnit", "top"}
        Dim ldifSTR As String = "ou=prova.com,dc=mailenable,dc=local"
        With oRequest
            .Attributes.Add(New nmsLDAP.DirectoryAttribute("objectClass", ldifOCS))
            .Attributes.Add(New nmsLDAP.DirectoryAttribute("ou", "prova.com"))
            .DistinguishedName = ldifSTR
        End With
        ''creo la response della request appena creata
        Try
            Dim oResponse As nmsLDAP.AddResponse = DirectCast(oConnection.SendRequest(oRequest), nmsLDAP.AddResponse)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
 
Back
Top