Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从LDAP获取用户的完整列表_C#_Ldap - Fatal编程技术网

C# 从LDAP获取用户的完整列表

C# 从LDAP获取用户的完整列表,c#,ldap,C#,Ldap,我尝试使用下面的代码获取用户的完整列表。但我得到的代码是“无法联系服务器” 有什么想法吗 谢谢 static void Main(string[] args) { string groupName = "Domain Users"; string domainName = "LDAP://ldap.mycompany.be:389/ou=users,o=mycompany,dc=mycompany,dc=be"; PrincipalContext ctx = new P

我尝试使用下面的代码获取用户的完整列表。但我得到的代码是“无法联系服务器”

有什么想法吗

谢谢

static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "LDAP://ldap.mycompany.be:389/ou=users,o=mycompany,dc=mycompany,dc=be";

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);

    if (grp != null)
    {
        foreach (Principal p in grp.GetMembers(false))
        {
            Console.WriteLine(String.Format("{0} - {1}", p.SamAccountName, p.DisplayName));
        }


        grp.Dispose();
        ctx.Dispose();
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
        Console.ReadLine();
    }
}
更新:此代码正在工作(来自同一台机器)

更新2

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=System.DirectoryServices.AccountManagement
  StackTrace:
       at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
       at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
       at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
       at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name)
       at MoulinetteUser.Program.Main(String[] args) in C:\Users\.....\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

您的问题是PrincipalContext的参数不正确:您正在以domainName传递LDAP查询,而不是域控制器的名称和端口。看那节课

您的第二个代码帖子之所以有效,是因为您使用的类是LDAP客户机类,并且它“理解”您的LDAP查询

尝试以下操作,看看是否有效:

static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "ldap.mycompany.be"; // or whatever your domain controller's name is...

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);

    if (grp != null)
    {
        foreach (Principal p in grp.GetMembers(false))
        {
            Console.WriteLine(String.Format("{0} - {1}", p.SamAccountName, p.DisplayName));
        }


        grp.Dispose();
        ctx.Dispose();
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
        Console.ReadLine();
    }
}

希望对您有所帮助……

您是否使用开发人员机器上的LDAP工具尝试了该连接字符串?请提供更多详细信息。你能用LDAP工具浏览吗?有防火墙吗?以此类推……在“PrincipalContext ctx”行中,我在几秒钟后出现了这个错误“对象引用未设置为对象的实例”。感谢您的帮助。在这一行中:PrincipalContext ctx=new PrincipalContext(ContextType.Domain,domainName)?异常跟踪是什么?机器是域的一部分吗?不在域中,但我想,我的第二个示例正在同一台机器上工作,一个到ldap.mycompany.com的跟踪器给了我正确的结果。是的,但这两个代码确实不同:您的第二个示例只联系“ldap部分”你的广告。你不需要是目录的一部分,这样做。PrincipalContext的第一个代码示例取决于AD,我相信(尽管需要确认)需要客户机成为域的一部分。。。简单地说,您没有使用LDAP来查询第一个示例中的广告。
static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "ldap.mycompany.be"; // or whatever your domain controller's name is...

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);

    if (grp != null)
    {
        foreach (Principal p in grp.GetMembers(false))
        {
            Console.WriteLine(String.Format("{0} - {1}", p.SamAccountName, p.DisplayName));
        }


        grp.Dispose();
        ctx.Dispose();
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
        Console.ReadLine();
    }
}