Показать сообщение отдельно
Старый 22.09.2008, 13:06   #7  
_scorp_ is offline
_scorp_
Участник
Аватар для _scorp_
MCBMSS
 
488 / 369 (13) ++++++
Регистрация: 25.07.2007
Адрес: Москва
Как то писал такую вещь на C#. Собирает всех пользователей в AD через LDAP. Вот используемые классы:
X++:
class Connection
    {
        public System.DirectoryServices.DirectoryEntry root;
        public Connection(String connectString)
        {
            this.root = new System.DirectoryServices.DirectoryEntry(connectString);
        }
    }
 
class MyComparator : System.Collections.IComparer
    {
        int System.Collections.IComparer.Compare(object a, object b)
        {
            User u1 = (User)a;
            User u2 = (User)b;
            int res = u1.city.CompareTo(u2.city);
            if (res == 0)
            {
                res = u1.department.CompareTo(u2.department);
                if (res == 0)
                    res = u1.fio.CompareTo(u2.fio);
            }
            return res;
        }
    }
 
class User
    {
        public String fio;
        public String city;
        public String department;
        public String position;
        public String internalNumber;
        public String externalNumber;
        public String email;
        public String icq;
        public String skype;
        public String birthday;
        public User() 
        {             
            this.fio = "";
            this.city = "";
            this.department = ""; 
            this.position = "";
            this.internalNumber = "";
            this.externalNumber = "";
            this.email = "";
            this.icq = "";
            this.skype = "";
            this.birthday = "";
        }
        public void fill(System.DirectoryServices.DirectoryEntry elem)
        {
            try
            {   this.fio = elem.Properties["displayName"].Value.ToString(); }
            catch
            {   this.fio = "";  }
            try
            {   this.city = elem.Properties["l"].Value.ToString(); }
            catch
            {   this.city = ""; }
            try
            {   this.department = elem.Properties["department"].Value.ToString(); }
            catch
            {   this.department = "";   }
            try
            {   this.position = elem.Properties["title"].Value.ToString();    }
            catch
            {   this.position = "";  }
            try
            {   this.internalNumber = elem.Properties["telephoneNumber"].Value.ToString();    }
            catch
            {   this.internalNumber = "";   }
            try
            {   this.externalNumber = elem.Properties["homePhone"].Value.ToString();    }
            catch
            {   this.externalNumber = "";   }
            try
            {   this.email = elem.Properties["mail"].Value.ToString();  }
            catch
            {   this.email = "";   }
            try
            {   this.icq = elem.Properties["pager"].Value.ToString();   }
            catch
            {   this.icq = "";  }
            try
            {   this.skype = elem.Properties["ipPhone"].Value.ToString();  }
            catch
            {   this.skype = "";    }
        }
    }
 
[Flags]
    public enum AdsUserFlags
    {
        Script = 1,                  // 0x1
        AccountDisabled = 2,              // 0x2
        HomeDirectoryRequired = 8,           // 0x8 
        AccountLockedOut = 16,             // 0x10
        PasswordNotRequired = 32,           // 0x20
        PasswordCannotChange = 64,           // 0x40
        EncryptedTextPasswordAllowed = 128,      // 0x80
        TempDuplicateAccount = 256,          // 0x100
        NormalAccount = 512,              // 0x200
        InterDomainTrustAccount = 2048,        // 0x800
        WorkstationTrustAccount = 4096,        // 0x1000
        ServerTrustAccount = 8192,           // 0x2000
        PasswordDoesNotExpire = 65536,         // 0x10000
        MnsLogonAccount = 131072,           // 0x20000
        SmartCardRequired = 262144,          // 0x40000
        TrustedForDelegation = 524288,         // 0x80000
        AccountNotDelegated = 1048576,         // 0x100000
        UseDesKeyOnly = 2097152,            // 0x200000
        DontRequirePreauth = 4194304,          // 0x400000
        PasswordExpired = 8388608,           // 0x800000
        TrustedToAuthenticateForDelegation = 16777216, // 0x1000000
        NoAuthDataRequired = 33554432         // 0x2000000
    }
 
    class AllUsers
    {
        public System.Collections.ArrayList users;
        public AllUsers()
        {
            this.users = new System.Collections.ArrayList();
        }
        public void collectUsers(System.DirectoryServices.DirectoryEntry root)
        {
            foreach (System.DirectoryServices.DirectoryEntry elem in root.Children)
            {
                if (string.Compare(elem.SchemaClassName, "user") != 0)
                    this.collectUsers(elem);
                else
                {                    
                    AdsUserFlags userFlags = (AdsUserFlags)elem.Properties["userAccountControl"].Value;
                    if ((userFlags & AdsUserFlags.AccountDisabled) != AdsUserFlags.AccountDisabled)
                    {
                        User us = new User();
                        us.fill(elem);
                        this.users.Add(us);
                    }
                }
            }
        }
    }
А вот собственно использование:
X++:
class Program
    {
        static void Main(string[] args)
        {       
            Connection con = new Connection(@"LDAP://bla-bla-bla");
            AllUsers users = new AllUsers();
            users.collectUsers(con.root);
            users.users.Sort(new MyComparator());                               
        }
    }
В итоге класс users будет содержать всех пользователей из каталога который Вы укажите в строке подключения через LDAP.

Последний раз редактировалось _scorp_; 22.09.2008 в 13:14.