使用Ldap Connection从活动目录中检索所有用户 [英] Retriving all the user's from active directory using Ldap Connection

查看:139
本文介绍了使用Ldap Connection从活动目录中检索所有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我陷入了一个问题.我必须使用Ldap连接从活动目录中检索用户列表.我就是这样的:

 公共 静态  void  getUser()
    {
        DirectoryEntry directoryEntry =  DirectoryEntry("  + Environment.UserDomainName);
        字符串 userNames = " ;
         foreach ( directoryEntry.Children中的DirectoryEntry子
        {
            如果(child.SchemaClassName == " )
            {
                userNames + = child.Name + Environment.NewLine; // 使用换行符迭代并绑定所有用户
            }
        }
        Console.WriteLine(" );
        Console.WriteLine(userNames);
    } 



现在我的问题是如何使用Ldap Connection获取用户列表.

任何帮助将不胜感激..
感谢

解决方案

好吧,您在google的第一个结果中停了下来.您应该再看一下,就像这里:
http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory [ ^ ] ...,但其中有很多...

如果有帮助,请尝试使用此代码.

 使用( var  context =  new  PrincipalContext(ContextType.Domain," ))
{
    使用( var  searcher =  PrincipalSearcher(  UserPrincipal(上下文)))
    {
         foreach ( var 结果搜索器中.FindAll ())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as  DirectoryEntry;
            Console.WriteLine("  + de.Properties [  givenName"].Value);
            Console.WriteLine("  + de.Properties [  sn"].Value);
            Console.WriteLine("  + de.Properties [  samAccountName"].Value);
            Console.WriteLine("  + de.Properties [  userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine(); 


Hi all,

I stuck in a problem. I had to retrieve the list of user''s from active directory using Ldap connection.. I did this job using directory entry. This is how I did:

public static void getUser()
    {
        DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.UserDomainName);
        string userNames = "";
        foreach (DirectoryEntry child in directoryEntry.Children)
        {
            if (child.SchemaClassName == "User")
            {
                userNames += child.Name + Environment.NewLine; //Iterates and binds all user using a newline
            }
        }
        Console.WriteLine("************************Users************************");
        Console.WriteLine(userNames);
    }



Now my question is how we can get the list of user using Ldap Connection.

Any help will be appreciated..
Thanks

解决方案

Well, you stopped at google''s first result. You should have looked further, like here:
http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory[^]... but there are dozens of them...


Try this code if it helps.

using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();


这篇关于使用Ldap Connection从活动目录中检索所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆