HAVIT Knowledge Base

Vývoj webových aplikací, .NET, SQL, návrh
Welcome to HAVIT Knowledge Base Sign in | Join | Help
-
Home Články Forums Obrázky Soubory

.NET Framework

Microsoft .NET Framework, Base Class Library

Načítání uživatelů z Active Directory

Jen krátký kousek kódu, který možná někdy ušetří pár minut nad dokumentací:

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace ActiveDirectorySynchronizer
{
    class Program
    {
        // doména test.branik.havit.cz, Organizational Unit: My Organizational Unit, pod tím OU: SubUnit
        public static string rootPath = "LDAP://OU=SubUnit,OU=My Organizational Unit,DC=test,DC=branik,DC=havit,DC=cz";
        public static string usernameOptional = @"test.branik.havit.cz\Administrator";
        public static string passwordOptional = "P@ssw0rd";

        static void Main(string[] args)
        {
            // node, od kterého se mají uživatelé hledat
            DirectoryEntry searchRoot = new DirectoryEntry(rootPath);
            if (!String.IsNullOrEmpty(usernameOptional))
            {
                searchRoot.Username = usernameOptional;
                searchRoot.Password = passwordOptional;
            }

            // vyhledání všech uživatelů
            DirectorySearcher ds = new DirectorySearcher(searchRoot, "(objectClass=user)");
            ds.PropertiesToLoad.Add("displayName");
            ds.PropertiesToLoad.Add("sAMAccountName");
            ds.PropertiesToLoad.Add("objectGUID");

            foreach (SearchResult sr in ds.FindAll())
            {
                Guid userGuid = new Guid((byte[])sr.Properties["objectGUID"][0]);
                string userDisplayName = String.Empty;

                Console.Write(userGuid);

                if (sr.Properties["displayName"].Count > 0)
                {
                    userDisplayName = sr.Properties["displayName"][0].ToString();
                    Console.Write(",");
                    Console.Write(userDisplayName);
                }


                Console.WriteLine();
            }

            Console.WriteLine("DONE");
        }
    }
}

Published 9. srpna 2007 13:52 by Robert Haken
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

FASTER said:

To neni zcela spravne, to funguje pouze do 1000 uzivatelu....

http://fastersolutions.blogspot.com/2007/07/directorysearcher-vrac-maximln-1000.html

srpna 9, 2007 18:45

What do you think?

(required) 
(optional)
(required) 
Enter the code you see below

Submit