/// <summary> /// Gets the LDAP users from the LDAP server. /// </summary> /// <param name="ldapServer">The LDAP server, string format: "LDAP://172.22.100.10:389/OU=AT,O=ON"</param> /// <param name="directoryType">Type of the directory.</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <param name="domain">The domain (AD only).</param> /// <returns>String list of LDAP users.</returns> /// <remarks>Documented by Dev09, 2009-06-08</remarks> public List <string> GetLdapUsers(string ldapServer, LocalDirectoryType directoryType, string user, string password, string domain) { List <string> LdapUsers = new List <string>(); switch (directoryType) { case LocalDirectoryType.ActiveDirectory: if (String.IsNullOrWhiteSpace(domain)) { string username = WindowsIdentity.GetCurrent().Name; domain = username.Substring(0, username.IndexOf(@"\")); } PrincipalContext context; if (!String.IsNullOrWhiteSpace(user) && !String.IsNullOrWhiteSpace(password) && !String.IsNullOrWhiteSpace(domain)) { context = new PrincipalContext(ContextType.Domain, domain, user, password); } if (!String.IsNullOrWhiteSpace(domain)) { context = new PrincipalContext(ContextType.Domain, domain); } else { context = new PrincipalContext(ContextType.Domain); } UserPrincipal userP = new UserPrincipal(context); userP.Enabled = true; PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = userP; PrincipalSearchResult <Principal> result = pS.FindAll(); foreach (Principal p in result) { LdapUsers.Add(domain + "\\" + p.SamAccountName); } break; case LocalDirectoryType.eDirectory: string serverName = Regex.Match(ldapServer, @"^.+//(.+?):").Groups[1].ToString(); string distinguishedName = ldapServer.Substring(ldapServer.LastIndexOf("/") + 1); LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(serverName)); connection.AuthType = AuthType.Basic; // attempt to connect try { connection.Bind(new NetworkCredential(user, password)); } catch (Exception exception) { Trace.WriteLine(exception.ToString()); } // run search for users SearchResponse response = connection.SendRequest(new SearchRequest(distinguishedName, "(|(objectClass=person)(objectClass=user))", System.DirectoryServices.Protocols.SearchScope.Subtree, null)) as SearchResponse; foreach (SearchResultEntry entry in response.Entries) { if (entry.Attributes.Contains("cn") && entry.Attributes["cn"][0].ToString() != String.Empty) { LdapUsers.Add("cn=" + entry.Attributes["cn"][0].ToString()); } } break; } return(LdapUsers); }
/// <summary> /// Gets the LDAP users from the LDAP server. /// </summary> /// <param name="ldapServer">The LDAP server, string format: "LDAP://172.22.100.10:389/OU=AT,O=ON"</param> /// <param name="directoryType">Type of the directory.</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <param name="domain">The domain (AD only).</param> /// <returns>String list of LDAP users.</returns> /// <remarks>Documented by Dev09, 2009-06-08</remarks> public List<string> GetLdapUsers(string ldapServer, LocalDirectoryType directoryType, string user, string password, string domain) { List<string> LdapUsers = new List<string>(); switch (directoryType) { case LocalDirectoryType.ActiveDirectory: if (String.IsNullOrWhiteSpace(domain)) { string username = WindowsIdentity.GetCurrent().Name; domain = username.Substring(0, username.IndexOf(@"\")); } PrincipalContext context; if (!String.IsNullOrWhiteSpace(user) && !String.IsNullOrWhiteSpace(password) && !String.IsNullOrWhiteSpace(domain)) context = new PrincipalContext(ContextType.Domain, domain, user, password); if (!String.IsNullOrWhiteSpace(domain)) context = new PrincipalContext(ContextType.Domain, domain); else context = new PrincipalContext(ContextType.Domain); UserPrincipal userP = new UserPrincipal(context); userP.Enabled = true; PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = userP; PrincipalSearchResult<Principal> result = pS.FindAll(); foreach (Principal p in result) LdapUsers.Add(domain + "\\" + p.SamAccountName); break; case LocalDirectoryType.eDirectory: string serverName = Regex.Match(ldapServer, @"^.+//(.+?):").Groups[1].ToString(); string distinguishedName = ldapServer.Substring(ldapServer.LastIndexOf("/") + 1); LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(serverName)); connection.AuthType = AuthType.Basic; // attempt to connect try { connection.Bind(new NetworkCredential(user, password)); } catch (Exception exception) { Trace.WriteLine(exception.ToString()); } // run search for users SearchResponse response = connection.SendRequest(new SearchRequest(distinguishedName, "(|(objectClass=person)(objectClass=user))", System.DirectoryServices.Protocols.SearchScope.Subtree, null)) as SearchResponse; foreach (SearchResultEntry entry in response.Entries) { if (entry.Attributes.Contains("cn") && entry.Attributes["cn"][0].ToString() != String.Empty) { LdapUsers.Add("cn=" + entry.Attributes["cn"][0].ToString()); } } break; } return LdapUsers; }