Code Monkey home page Code Monkey logo

directoryhelper's Introduction

DirectoryHelper

C# .NET Helper library for interaction with Active Directory objects such as users, contacts, groups (based on the DirectoryEntry class)

Nuget

Latest package available here

Summary

This library was written to provide some useful wrappers around common tasks I've encountered when dealing with LDAP and DirectoryEntry/DirectorySearcher classes in the System.DirectoryServices namespace.

Sample

To get things started, here's a code sample of the usage for a few of the types and extension methods provided:

namespace InitiateNorth.DirectoryHelper.Sample
{
    using System.DirectoryServices;
    using System.Linq;
    using InitiateNorth.DirectoryHelper;

    /// <summary>
    /// Class demonstrates the sample usage of the DirectoryHelper library.
    /// </summary>
    public class Program
    {
        
        /// <summary>
        /// Main entry point for the program.
        /// </summary>
        /// <param name="args">Command line arguments (ignored by this method).</param>
        public static void Main(string[] args)
        {
            var searchTerm = "Testy McTestington";

            // Removing primative obsession
            var fqdnResult = Fqdn.Create("internal.domain.local");

            if (fqdnResult.IsSuccess)
            {
                var fqdn = fqdnResult.Value;

                // Use the extensions to give us an LDAP connection string.
                using (var entry = new DirectoryEntry(fqdn.ToLdapConnectionString()))
                using (var searcher = new DirectorySearcher(entry))
                {
                    // Set the search filter up using the attributes, look for people with display name that matches the search term.
                    searcher.Filter = $"(&({DirectoryAttributes.ObjectClass}=person)({DirectoryAttributes.ObjectCategory}=person)({DirectoryAttributes.DisplayName}={searchTerm}))";

                    // No limit on the page size, so we don't have to page the results.
                    searcher.PageSize = 999;

                    // Add some properties we want to see on the returned results.
                    searcher.PropertiesToLoad.AddRange(
                        new string[]
                        {
                            DirectoryAttributes.DistinguishedName,
                            DirectoryAttributes.ObjectSid,
                            DirectoryAttributes.SamAccountName,
                            DirectoryAttributes.MemberOf,
                            DirectoryAttributes.DisplayName,
                            DirectoryAttributes.Mail,
                        });

                    using (var results = searcher.FindAll())
                    {
                        /* 
                         * Sadly the directory stuff doesn't implement any LINQ related goodness, 
                         * and we have to tell the compiler what we're expecting (a SearchResult from a SearchResultCollection)
                         */
                        foreach (SearchResult result in results)
                        {
                            var mail = result.Properties.GetReference<string>(DirectoryAttributes.Mail);
                            var displayName = result.Properties.GetReference<string>(DirectoryAttributes.DisplayName);
                            var memberOf = result.Properties.GetCollectionReference<string>(DirectoryAttributes.MemberOf).ToList(); // Force evaluation now so you can have a poke about.
                            var objectSid = result.Properties.GetReference<byte[]>(DirectoryAttributes.ObjectSid);
                            // ... etc. etc.

                            /* 
                             * NOTE: Be aware that if you are retrieving a group and want to check the 'members'
                             * property for example, AD will limit the results to 1500 per 'page' unless you are
                             * doing an 'attribute scope query'.
                             */

                            // Some examples of other extensions being used to get a SID
                            if (objectSid.IsSidResolvable())
                            {
                                var readableSid = objectSid.ToSidString();
                                var domainAndUser = objectSid.ToResolvedDomainAndUser();
                            }

                            if (result.GetDirectoryEntry().IsContactObject())
                            {
                                // This person is a contact, not a user.
                            }
                        }
                    }
                }
            }
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.