Sirwan Afifi
Sirwan Afifi

Reputation: 10824

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
how can I do that?

Upvotes: 2

Views: 10409

Answers (2)

Sampath
Sampath

Reputation: 65988

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:

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

and LINQ to LDAP

For .NET Core or Standard, please see Chris D's answer, below.

For get comprehensive knowledge about this subject check (Almost) Everything In Active Directory via C#

I hope this will help to you.

Upvotes: 8

Chris D
Chris D

Reputation: 134

Sorry to reply to such an old question, but I felt it needed an updated answer. I wrote a .NET Standard library for this:

  • Linq2Ldap.Core (NuGet, GitHub) The platform-independent core library that can translate from Expressions into LDAP filters, and parse them back out, again.

It has two wrapper libraries for Active Directory:

  • Linq2Ldap.Protocols (NuGet, GitHub) - A thin wrapper around Linq2Ldap.Core + System.DirectoryServices.Protocols.
  • Linq2Ldap (NuGet, GitHub) - A thin wrapper for System.DirectoryServices.

The heart of it can transpile between Expression<Func<T, bool>>s and LDAP filters. 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". You can also create special properties to alias your directory attributes, too. Please see the project wiki for more info.

Upvotes: 0

Related Questions