2

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?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110

2 Answers2

8

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.

Chris D
  • 134
  • 1
  • 10
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • thanks a lot my friend,it's great,i have another question : your code runs on server with AD,i can run this app on another computer(a computer that joined to domain) what's the LDAP address in this case? – Sirwan Afifi Jan 14 '13 at 05:05
  • @SirwanAfifi Glad to hear that it helped.For your question you can refer this article http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C – Sampath Jan 14 '13 at 07:40
  • @Sampath Would you recommend using LINQ to LDAP? – user2609980 Feb 21 '14 at 10:07
  • @user2609980 Yes.If you can read this, you can realize that too.http://linqtoldap.codeplex.com/ – Sampath Feb 21 '14 at 10:35
  • @Sampath can you help me with my question [here](http://stackoverflow.com/questions/21939802/using-linq-to-ldap-to-query-the-active-directory-how-to-define-directoryschema)? I do not understand nor can I find how to define the `DirectorySchema`. – user2609980 Feb 21 '14 at 20:29
0

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.

Chris D
  • 134
  • 1
  • 10