使用c# DirectoryServices访问活动目录时出现问题

本文关键字:问题 DirectoryServices 访问 活动 使用 | 更新日期: 2023-09-27 18:08:20

  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();

我从我的c#表单应用程序使用上述代码。每当我调用FindAll()时,我都会得到一个异常,如下所示。

System.Runtime.InteropServices。COMException未处理
消息="未知错误(0x80005000)"
源= "系统。DirectoryServices"ErrorCode = -2147463168
加:System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)在System.DirectoryServices.DirectoryEntry.Bind ()在System.DirectoryServices.DirectoryEntry.get_AdsObject ()System.DirectoryServices.DirectorySearcher.FindAll(布尔findMoreThanOne)在System.DirectoryServices.DirectorySearcher.FindAll ()在LDAPApp.Form1。点击(对象发送者,EventArgs e) inH: ' Raj ' LDAP ' LDAPApp ' LDAPApp ' Form1.cs: 37在System.Windows.Forms.Control。OnClick (EventArgs e)在System.Windows.Forms.Button。OnClick (EventArgs e)在System.Windows.Forms.Button。OnMouseUp (MouseEventArgs mevent)在System.Windows.Forms.Control.WmMouseUp (Message&米,鼠标按钮,点击32次)在System.Windows.Forms.Control.WndProc (Message&米)在System.Windows.Forms.ButtonBase.WndProc (Message&米)在System.Windows.Forms.Button.WndProc (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message&米)System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtrhWnd, Int32 msg, IntPtr wparam, IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG&味精)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32dwComponentID, Int32 reason, Int32 pvLoopData)在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application。运行(mainForm形式)在ldappapp . program . main (H: ' Raj ' LDAP ' LDAPApp ' LDAPApp ' Program.cs:第18行在System.AppDomain。_nExecuteAssembly(组装装配,String [] args)在System.AppDomain。assemblyFile ExecuteAssembly(字符串,证据程序集安全,字符串[]参数)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)System.Threading.ExecutionContext.Run (ExecutionContextexecutionContext, ContextCallback, callback, Object state)在System.Threading.ThreadHelper.ThreadStart ()
InnerException:

请告诉我我哪里做错了。

使用c# DirectoryServices访问活动目录时出现问题

LDAP路径中的LDAP协议标识符(LDAP://)必须为大写。如果将LDAP协议标识符写成小写,则会得到0x80005000错误代码(未知错误)。下面的代码片段应该可以工作:

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();

希望有帮助。