简体   繁体   English

如何使用LINQ to LDAP获取活动目录中的用户列表?

[英]how can get List of users in active directory using LINQ to LDAP?

I want to access to ActiveDirectory using LINQ to LDAP and I want to Get List of All users in that 我想使用LINQ to LDAP来访问ActiveDirectory,并且想要获取其中的所有用户列表
how can I do that? 我怎样才能做到这一点?

You can try something like below. 您可以尝试以下类似方法。

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; }

}

Use this code to access AD from a console app, placing your AD server in the below code: 使用此代码从控制台应用程序访问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;
}

For more information check Get All Users using C# with Linq To Active Directory 有关更多信息,请检查“将所有使用C#的用户与Linq一起使用到Active Directory”

and LINQ to LDAP LINQ to LDAP

For .NET Core or Standard , please see Chris D's answer , below. 对于.NET Core或Standard ,请参阅下面的Chris D的答案

For get comprehensive knowledge about this subject check (Almost) Everything In Active Directory via C# 有关此主题的全面知识,请通过C#检查(几乎)Active Directory中的所有内容

I hope this will help to you. 希望对您有帮助。

Sorry to reply to such an old question, but I felt it needed an updated answer. 很抱歉回答这样一个老问题,但我认为它需要更新的答案。 I wrote a .NET Standard library for this: 我为此编写了一个.NET标准库:

  • Linq2Ldap.Core ( NuGet , GitHub ) The platform-independent core library that can translate from Expressions into LDAP filters, and parse them back out, again. Linq2Ldap.Core( NuGetGitHub )与平台无关的核心库,可以将Expressions转换为LDAP过滤器,然后再次解析出来。

It has two wrapper libraries for Active Directory: 它具有两个Active Directory包装器库:

  • Linq2Ldap.Protocols ( NuGet , GitHub ) - A thin wrapper around Linq2Ldap.Core + System.DirectoryServices.Protocols. Linq2Ldap.Protocols( NuGetGitHub )-Linq2Ldap.Core + System.DirectoryServices.Protocols的瘦包装。
  • Linq2Ldap ( NuGet , GitHub ) - A thin wrapper for System.DirectoryServices. Linq2Ldap( NuGetGitHub )-System.DirectoryServices的精简包装。

The heart of it can transpile between Expression<Func<T, bool>> s and LDAP filters. 它的核心可以在Expression<Func<T, bool>>和LDAP过滤器之间转换。 The models ( T ) referenced by the Expressions must implement an interface, IEntry , that basically defines a fancy indexer class that you'd use like this: m => m["cn"] == "someuser" . Expressions引用的模型( T )必须实现一个接口IEntry ,该接口基本上定义了您要使用的精美索引器类,如下所示: m => m["cn"] == "someuser" You can also create special properties to alias your directory attributes, too. 您也可以创建特殊属性来别名目录属性。 Please see the project wiki for more info. 请参阅项目Wiki了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM