Problem while accessing active directory using C# DirectoryServices

0
  string ldapPath = "ldap://db.debian.org:389/uid=ma,ou=users,dc=debian,dc=org";
            DirectoryEntry dEntry = new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous);

            DirectorySearcher search = new DirectorySearcher(dEntry);

            search.Filter = "((objectClass=*))";
            search.FindAll();

I am using the above code from my C# forms application. Whenever i am calling the FindAll() I am getting an exception as below.

System.Runtime.InteropServices.COMException was unhandled
Message="Unknown error (0x80005000)"
Source="System.DirectoryServices" ErrorCode=-2147463168
StackTrace: at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) at System.DirectoryServices.DirectorySearcher.FindAll() at LDAPApp.Form1.button1_Click(Object sender, EventArgs e) in H:\Raj\LDAP\LDAPApp\LDAPApp\Form1.cs:line 37 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at LDAPApp.Program.Main() in H:\Raj\LDAP\LDAPApp\LDAPApp\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Please tell me what am i doing wrong here.

Thanks in advance.

c#
active-directory
directoryservices
asked on Stack Overflow Sep 13, 2011 by logeeks

1 Answer

2

The LDAP protocol identifier (LDAP://) in your LDAP path must be uppercase. If you write the LDAP protocol identifier in lowercase you get the 0x80005000 error code (unknown error). The following code snippet should work:

string ldapPath = "LDAP://db.debian.org:389/uid=ma,ou=users,dc=debian,dc=org";
        DirectoryEntry dEntry = new DirectoryEntry(ldapPath, null, null,  AuthenticationTypes.Anonymous);

DirectorySearcher search = new DirectorySearcher(dEntry);

search.Filter = "((objectClass=*))";
search.FindAll();

Hope, this helps.

answered on Stack Overflow Sep 13, 2011 by Hans

User contributions licensed under CC BY-SA 3.0