sexta-feira, 24 de julho de 2015

ActiveDirectoryProvider

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Configuration;

namespace Lapuinka.Security.ActiveDirectory
{
    public class ActiveDirectoryProvider : ActiveDirectoryObject
    {
        private readonly string _domain;

        public ActiveDirectoryProvider()
        {
            _domain = ConfigurationManager.AppSettings["ActiveDirectoryDomain"];
        }
       
        private DirectoryEntry Connect()
        {
            return Create(_domain);
        }

        private DirectoryEntry Connect(string path)
        {
            return Create(path);
        }

        public bool Authenticate(string userName, string password)
        {
            try
            {
                using (var entry = new DirectoryEntry(LdapBasePath + _domain, userName, password))
                {
                    return entry.NativeObject != null;
                }
            }
            catch (DirectoryServicesCOMException)
            {
                return false;
            }
            catch (COMException)
            {
                // User name is empty or null
                return false;
            }
        }

        public ActiveDirectoryUser GetUser(string userName)
        {
            using (var entry = Connect())
            {
                using (var mySearcher = new DirectorySearcher(entry))
                {
                    mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";

                    SearchResult result = mySearcher.FindOne();

                    if (result == null)
                    {
                        throw new NullReferenceException
                            ("unable to locate the distinguishedName for the object " +
                             userName + " in the domain");
                    }

                    return new ActiveDirectoryUser(result.GetDirectoryEntry());
                }
            }
        }

        public ActiveDirectoryGroup GetGroup(string name)
        {
            using (var entry = Connect())
            {
                using (var mySearcher = new DirectorySearcher(entry))
                {
                    mySearcher.Filter = "(&(objectClass=group)(|(cn=" + name + ")(dn=" + name + ")))";

                    SearchResult result = mySearcher.FindOne();

                    if (result == null)
                    {
                        throw new NullReferenceException
                            ("unable to locate the distinguishedName for the object " +
                             name + " in the domain");
                    }

                    return new ActiveDirectoryGroup(result.GetDirectoryEntry());
                }
            }
        }

        public IEnumerable GetUsers(string path)
        {
            using (var entry = Connect(path))
            {
                using (var mySearcher = new DirectorySearcher(entry))
                {
                    mySearcher.Filter = "objectClass=user";

                    var results = mySearcher.FindAll();

                    foreach (SearchResult result in results)
                    {
                        yield return new ActiveDirectoryUser(result.GetDirectoryEntry());
                    }
                }
            }
        }

        public IEnumerable<ActiveDirectoryGroup> GetGroups(string path)
        {
            using (var entry = Connect(path))
            {
                using (var mySearcher = new DirectorySearcher(entry))
                {
                    mySearcher.Filter = "objectClass=group";

                    var results = mySearcher.FindAll();

                    foreach (SearchResult result in results)
                    {
                        yield return new ActiveDirectoryGroup(result.GetDirectoryEntry());
                    }
                }
            }
        }

        public IEnumerable<ActiveDirectoryOrganizationalUnit> GetOrganizationalUnits(string path)
        {
            using (var entry = Connect(path))
            {
                using (var mySearcher = new DirectorySearcher(entry))
                {
                    mySearcher.Filter = "objectClass=organizationalUnit";

                    var results = mySearcher.FindAll();

                    foreach (SearchResult result in results)
                    {
                        yield return new ActiveDirectoryOrganizationalUnit(result.GetDirectoryEntry());
                    }
                }
            }
        }
    }
}


Postar um comentário