sexta-feira, 24 de julho de 2015

ActiveDirectoryObject C#

using System;
using System.DirectoryServices;
using System.Configuration;

namespace Lapuinka.Security.ActiveDirectory
{
    public abstract class ActiveDirectoryObject
    {
        protected static readonly string LdapBasePath;
        protected static readonly string LdapUserName;
        protected static readonly string LdapPassword;

        static ActiveDirectoryObject()
        {
            var host = ConfigurationManager.AppSettings["ActiveDirectoryHost"];

            LdapBasePath = String.IsNullOrEmpty(host) ? "LDAP://" : String.Format("LDAP://{0}/", host);

            LdapUserName = ConfigurationManager.AppSettings["ActiveDirectoryUserName"];
            LdapPassword = ConfigurationManager.AppSettings["ActiveDirectoryPassword"];
        }

        protected static DirectoryEntry Create(string path)
        {
            if (String.IsNullOrEmpty(LdapUserName))
            {
                return new DirectoryEntry(LdapBasePath + path);
            }
           
            return new DirectoryEntry(LdapBasePath + path, LdapUserName, LdapPassword);
        }
    }
}


Postar um comentário