【问题标题】:how can get List of users in active directory using LINQ to LDAP?如何使用 LINQ to LDAP 获取活动目录中的用户列表?
【发布时间】:2013-01-13 05:28:37
【问题描述】:

我想使用 LINQ to LDAP 访问 ActiveDirectory,并且我想获取其中所有用户的列表
我该怎么做?

【问题讨论】:

  • 我在我的帖子中包含了与此主题相关的非常有用的链接。请检查一下。

标签: c# winforms linq active-directory


【解决方案1】:

你可以试试下面的方法。

using ActiveDs;
using BdsSoft.DirectoryServices.Linq;
using System.Linq.Expressions;
using System.DirectoryServices;

[DirectorySchema( "user", typeof( IADsUser ) )]
class User
{
    public string Name { get; set; }

    public string sAMAccountName { get; set; }

    public string objectCategory { get; set; }

    public string mail { get; set; }

    public string Description { get; set; }

    [DirectoryAttribute( "PasswordLastChanged", DirectoryAttributeType.ActiveDs )]
    public DateTime PasswordLastSet { get; set; }

    [DirectoryAttribute("distinguishedName")]
    public string Dn { get; set; }

    [DirectoryAttribute("memberOf")]
    public string[] Groups { get; set; }

}

使用此代码从控制台应用程序访问 AD,将您的 AD 服务器放置在以下代码中:

static void Main( string[] args )
{

    IEnumerable<User> users = GetADUsers();

    Console.WriteLine( "Users: " + users.Count().ToString() );

}

static DirectoryEntry ROOT = new DirectoryEntry( "LDAP://MyADDomainLocation.com" );

private static IEnumerable<User> GetADUsers()
{
    IEnumerable<User> users;

    var usersDS = new DirectorySource<User>( ROOT, SearchScope.Subtree );

            users = from usr in usersDS
                    where usr.Name == "A*" // FIlter A then any character(s)
                    select usr;

     users = users.OrderBy( user => user.Name ).ToList(); // Sort them alphabetically by name.

    return users;
}

更多信息请查看Get All Users using C# with Linq To Active Directory

LINQ to LDAP

对于 .NET Core 或 Standard,请参阅下面的 Chris D's answer

要全面了解该主题,请查看(Almost) Everything In Active Directory via C#

希望对你有帮助。

【讨论】:

  • 非常感谢我的朋友,太好了,我还有一个问题:您的代码在带有 AD 的服务器上运行,我可以在另一台计算机(加入域的计算机)上运行此应用程序 LDAP 地址是什么在这种情况下?
  • @SirwanAfifi 很高兴听到它有帮助。对于您的问题,您可以参考这篇文章codeproject.com/Articles/18102/…
  • @Sampath 你会推荐使用 LINQ to LDAP 吗?
  • @user2609980 是的。如果你能读到这个,你也能意识到这一点。linqtoldap.codeplex.com
  • @Sampath 你能帮我解决我的问题here 吗?我不明白,也找不到如何定义DirectorySchema
【解决方案2】:

很抱歉回答这么老的问题,但我觉得它需要一个更新的答案。我为此编写了一个 .NET Standard 库:

  • Linq2Ldap.Core (NuGet, GitHub) 独立于平台的核心库,可以将表达式转换为 LDAP 过滤器,并将它们再次解析出来。

它有两个用于 Active Directory 的包装库:

  • Linq2Ldap.Protocols (NuGet, GitHub) - Linq2Ldap.Core + System.DirectoryServices.Protocols 的薄包装。
  • Linq2Ldap (NuGet, GitHub) - System.DirectoryServices 的精简包装器。

它的核心可以在 Expression&lt;Func&lt;T, bool&gt;&gt;s 和 LDAP 过滤器之间转换。表达式引用的模型 (T) 必须实现一个接口 IEntry,它基本上定义了一个花哨的索引器类,您可以像这样使用它:m =&gt; m["cn"] == "someuser"。您也可以创建特殊属性来为您的目录属性设置别名。请参阅项目 wiki 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多