Search code examples
c#ldapopends

Connecting to OpenDS using System.DirectoryService


I have been trying to find some tutorials on how to connect to OpenDS using .NET's LDAP types to no avail. Can anyone point me to some articles/tutorials that have good samples on using OpenDS as a directory service and accessing and working with it using C#.

This is what I have tried so far, but always get an invalid username/password error. I'm stuck at what credentials need to go in or if what i'm trying to do makes any sense at all.

DirectoryEntry directoryEntry = new DirectoryEntry
                                            {
                                                Path = @"LDAP://SUnnikris-va-d:389/dc=example,dc=com",
                                                Username = "uid=user.0",
                                                Password = "TestPass!",
                                                AuthenticationType = AuthenticationTypes.ServerBind
                                            };

        directoryEntry.RefreshCache();

        DirectoryEntry newUser = directoryEntry.Children.Add("uid=nuser,ou=People,dc=example,dc=com", "person");
        newUser.Properties["objectClass"].Value = new object[] { "top", "person", "organizationalPerson", "inetorgPerson" };
        newUser.Properties["uid"].Value = "nuser";
        newUser.Properties["givenName"].Value = "new";
        newUser.Properties["sn"].Value = "user";
        newUser.Properties["cn"].Value = "new user";
        newUser.Properties["userPassword"].Value = "nuser";
        newUser.CommitChanges();

Solution

  • I figured it out, OpenDS uses the canonical name as the superuser for administration. Essentially, the problem was with the credentials I was using, instead of a uid I had to specify this:-

    NetworkCredential myCreds = new NetworkCredential("cn=Directory Manager", "TestPass!");