Monday, April 9, 2012

Active Directory for getting users


Active Directory
Active Directory (AD) is a directory service created by Microsoft.
Active Directory uses a number of standardized protocols to provide a variety of network services, including:
·        Lightweight Directory Access Protocol LDAP, the industry standard directory access protocol, compatible with many management and query applications. Active Directory supports LDAPv3 and LDAPv2.
·        Optional Kerberos-based authentication
·        DNS-based naming and other network information
Code

Add a reference System.DirectoryServices and include in the application.

Step (i)  
       //It will automatically Get current Domain name
        DirectoryEntry direntry = new DirectoryEntry();
Or
       String strDomainName = "yourdomain.com”
//It will Get specified Domain name       
DirectoryEntry entry = new DirectoryEntry("LDAP://" + strDomainName);

Step (ii)  
    //It will set specified Domain searcher
     DirectorySearcher dirsearcher = new DirectorySearcher(entry);

Step (iii)  
//It will filter the users from all searched details
dirsearcher.Filter = "(&(objectClass=user)(objectCategory=person))";

Step (iv)  
//Assigns all filtered collection to SearchResultCollection
SearchResultCollection sResult = dirsearcher.FindAll();

Step (iv)
//To get our values we need to assign to any string
if (null != sResult)
        {
            for (int i = 0; i < sResult.Count; i++)
            {
             //Storing in some collection
Strname[i] = sResult[i].Properties["name"][0].ToString();
Strnickname[i] = sResult[i].Properties["mailnickname"][0] .ToString();
Etc…
            }
       }

 
IIS Settings
·        Set webconfig authentication as windows
<authentication mode="Windows">
·        Set IIS Authentication->Windows Authentication to Enable  & Authentication->Anonymous Authentication to disable