1

All the questions are related to the .NET framework but not to the .NET Core. I am looking for how to get the all users information from AD group in NETCORE.

Lukas
  • 1,699
  • 1
  • 16
  • 49
Harsh
  • 115
  • 2
  • 6

3 Answers3

4

I'm using .Net Core 3.1 but you can use with .Net Core 2 as well.

First install the NuGet package "System.DirectoryServices.AccountManagement"

Then, you can use the code below to get all AD users:

using System.DirectoryServices.AccountManagement; 


public static List<ADUser> GetADUsers() {

    var myDomainUsers = new List<ADUser>();

    using (var ctx = new PrincipalContext(ContextType.Domain, "yourdomain"))
    {

        var userPrinciple = new UserPrincipal(ctx);

        using (var search = new PrincipalSearcher(userPrinciple))
        {
            foreach (UserPrincipal domainUser in search.FindAll().OrderBy(u => u.DisplayName))
            {
                var adUser = new ADUser() {
                    Description = domainUser.Description,
                    DisplayName = domainUser.DisplayName,
                    DistinguishedName = domainUser.DistinguishedName,
                    EmailAddress = domainUser.EmailAddress,
                    Name = domainUser.Name,
                    EmployeeId = domainUser.EmployeeId,
                    GivenName = domainUser.GivenName,
                    MiddleName = domainUser.MiddleName,
                    Surname = domainUser.Surname,
                    SamAccountName  = domainUser.SamAccountName
                }; 
                myDomainUsers.Add(adUser);
            } //foreach
        } //using
    } //using

    return myDomainUsers;

} //GetADGroups

Where I'm using the following ADUser class:

public class ADUser
{
    public string SamAccountName { get; set; }
    public string Description { get; set; }
    public string DisplayName { get; set; }
    public string DistinguishedName { get; set; }
    public string EmailAddress { get; set; }
    public string EmployeeId { get; set; }
    public string Name { get; set; }
    public string GivenName { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }

}

There are more attributes that you can extract from AD. Take a look in UserPrincipal class

Julio Schurt
  • 2,014
  • 2
  • 19
  • 21
2

I am connecting to Ldap using Novell.Directory.Ldap Package to authenticate my users.

Project.csproj

<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="2.3.8" />

Code.cs

using Novell.Directory.Ldap;

public bool LoginLdap(string username, string password)
{
    LdapConnection connection = new LdapConnection();
    var loggedIn = false;
    try
    {
         connection.Connect(_config["Ldap:url"], LdapConnection.DEFAULT_PORT);
         connection.Bind(LdapConnection.Ldap_V3, _config["Ldap:domain"] + @"\" + username, password);
         loggedIn = true;
    }
    catch 
    {
         loggedIn = false;
    }
    connection.Disconnect();
    return loggedIn;
}

Config.json

"Ldap": {
"url": "[Ldap URL]",
"domain": "[Domain Name]"
 }
Steve Tolba
  • 1,417
  • 1
  • 10
  • 11
1

If you only plan on running your application in Windows, you can add Microsoft.Windows.Compatibility to your project from NuGet, which includes the System.DirectoryServices namespace, so you can use DirectoryEntry/DirectorySearcher or the AccountManagement namespace like you can in the full .NET Framework.

But if you plan on running this on other OS's, then I think the only option is Novell's library, as Steve mentioned in his answer.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84