Search code examples
c#ldapopenldap

Facing Unknown error (0x80005000) while adding the user t LDAP in C#


I am facing Unknown error (0x80005000) while adding user to LDAP server(Apache), the following is my code. Could anyone please let me know where I am doing mistake.

namespace TestMethods
{
    public class Program
    {
        static void Main(string[] args)
        {
            var ldi = new LdapDirectoryIdentifier("localhost", 10389);
            AddUser("username", "o=Company");
        }
        public static void AddUser(string username, string group)
        {
            try
            {
                DirectoryEntry dirEntry = new 
                DirectoryEntry("LDAP://localhost:10389,o=Company" + group);
                Console.WriteLine("Added to the path");// Working 
                dirEntry.Invoke("Add", new object[] { username });//Received Exception here
                dirEntry.CommitChanges();
                Console.WriteLine("Added to the path");
                dirEntry.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Solution

  • I believe you should use a / to separate the server name from the DN in your path:

    LDAP://localhost:10389/o=Company
    

    The constructor of DirectoryEntry doesn't make any network requests, so your path isn't validated until you actually use it.

    However, if you are not using Active Directory, then I don't think Invoke will work for you. The description of DirectoryEntry.Invoke says:

    Calls a method on the native Active Directory Domain Services object.

    Even then, I'm not sure which Add method you're trying to use.

    The way to create a new object using DirectoryEntry is like this (assuming dirEntry is pointing to a path where it can be created):

    var newUser = dirEntry.Children.Add($"uid={username}", "inetOrgPerson");
    
    // Set other attributes like this:
    // newUser.Properties["someAttribute"].Value = "something";
    
    //Save - this is where the object is actually created
    newUser.CommitChanges();
    

    I've never used Apache's LDAP server (I know AD better), so you may have to edit the schema ("inetOrgPerson") if you need to.