Search active users from AD

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IrraUk
    New Member
    • Jun 2017
    • 1

    Search active users from AD

    Code:
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Select active users from AD:");
                using (var context = new PrincipalContext(ContextType.Domain, "mydomain.com.nt", "OU=Departments,DC=com,DC=nt"))
                {   
                    DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://mydomain.com.nt/OU=Departments,DC=or,DC=nt", @"User_name", "password", AuthenticationTypes.Delegation);
                    DirectorySearcher search = new DirectorySearcher(ldapConnection);
                    search.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:= 2))";
                  search.PropertiesToLoad.Add("distinguishedName");
                    search.PropertiesToLoad.Add("whenCreated");
                    search.PropertiesToLoad.Add("description");
                    search.PropertiesToLoad.Add("operatingSystem");
                    search.PropertiesToLoad.Add("name");
                    SearchResult result;
                    List<Users> lstADUsers = new List<Users>();
                    SearchResultCollection resultCol = search.FindAll();
                    if (resultCol != null)
                    {
        for (int counter = 0; counter < resultCol.Count; counter++)
                        {
           string UserNameEmailString = string.Empty;
                            result = resultCol[counter];
                            if (result.Properties.Contains("samaccountname") &&
                                     result.Properties.Contains("mail") &&
                                result.Properties.Contains("displayname"))
                            {
            Users objSurveyUsers = new Users();
                objSurveyUsers.Email = (String)result.Properties["mail"][0] +
                                  "^" + (String)result.Properties["displayname"][0];
                                objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
                                objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
                                lstADUsers.Add(objSurveyUsers);
                            }
                        }
                    }
                }
            }
            public class Users
            {
                public string Email { get; set; }
                public string UserName { get; set; }
                public string DisplayName { get; set; }
                public bool isMapped { get; set; }
            }
        }
    }
    Hi all! Help me, please with this task.
    I try connect to remote AD from pc, witch is not in one domain with Active Directory. I do this in VS 2017. When I start this console app, here no exceptions or mistakes. But I don't get list of active users from AD.
    Where is my mistake? Explain me, please.
    Thanks
Working...