Download source code by Clicking here
Authenticate users accessing application by using Microsoft Active Directory using Microsoft DirectoryServices.
Security is one of the most important thing while developing an application for an Enterprise. Active Directory is a directory service used to store information about the network resources across a domain and also centralize the network. Using Active directory authentication you can allow users under a domain to access application along with the roles as built in active directory.
Using the code
To use Active Directory authentication DirectoryEntry and DirectorySearcher classes will be used, these classes are part of Microsoft DirectoryServices.
First Add reference to .Net Interop System.DirectoryServices then create instance of DirectoryEntry and DirectorySearcher Class.
Then made a search in active directory against your login credentials, if searchresult returns object of the user it means that user exist in the Active Directory with the submitted credentials.
The source code also contains a method "GetDomains" which returns an ArrayList having all the domains that are present in the network.
1: public ArrayList GetDomains()
3: ArrayList arrDomains = new ArrayList();
4: DirectoryEntry ParentEntry = new DirectoryEntry();
7: ParentEntry.Path = "WinNT:";
8: foreach (DirectoryEntry childEntry in ParentEntry.Children)
10: switch (childEntry.SchemaClassName)
12: case "Domain":
21: }} }
22: catch (Exception e)
27: ParentEntry = null;
29: return arrDomains;