/// <summary>
 /// Gets all domain users.
 /// </summary>
 /// <returns></returns>
 public static string[] GetAllDomainUsers()
 {
     try
     {
         List <string>  upns = new List <string>();
         DirectoryEntry root = DirectoryServicesUtils.newDirectoryEntry("LDAP://" + SqlAzManStorage.RootDSEPath);
         root.RefreshCache();
         DirectorySearcher ds = new DirectorySearcher(root, String.Format("(&(objectClass={0}))", "user"));
         ds.PropertiesToLoad.Add("userPrincipalName");
         SearchResultCollection src = ds.FindAll();
         foreach (SearchResult sr in src)
         {
             if (sr.Properties["userPrincipalName"] != null && sr.Properties["userPrincipalName"].Count > 0)
             {
                 string upn = sr.Properties["userPrincipalName"][0].ToString();
                 upns.Add(upn);
             }
         }
         upns.Sort();
         return(upns.ToArray());
     }
     catch
     {
         return(new string[0]);
     }
 }
 /// <summary>
 /// Executes the LDAP query.
 /// </summary>
 /// <param name="lDapQuery">The l dap query.</param>
 /// <returns></returns>
 public static SearchResultCollection ExecuteLDAPQuery(string lDapQuery)
 {
     try
     {
         DirectoryEntry root = DirectoryServicesUtils.newDirectoryEntry("LDAP://" + (DirectoryServicesUtils.GetRootDSEPart(lDapQuery) ?? SqlAzManStorage.RootDSEPath));
         root.RefreshCache();
         DirectorySearcher searcher = new DirectorySearcher(root, DirectoryServicesUtils.GetLDAPQueryPart(lDapQuery), new string[] { "objectSid" });
         return(searcher.FindAll());
     }
     catch
     {
         throw;
     }
 }
 static DirectoryServicesUtils()
 {
     try
     {
         DirectoryServicesUtils.userName = null;
         DirectoryServicesUtils.password = null;
         if (String.IsNullOrEmpty(DirectoryServicesUtils.rootDsePath))
         {
             DirectoryEntry rootDSE = DirectoryServicesUtils.newDirectoryEntry("LDAP://RootDSE");
             DirectoryServicesUtils.rootDsePath = (string)(rootDSE.Properties["defaultNamingContext"][0]);
             if (DirectoryServicesUtils.rootDsePath.ToUpper().StartsWith("LDAP://"))
             {
                 DirectoryServicesUtils.rootDsePath = DirectoryServicesUtils.rootDsePath.Substring(7);
             }
         }
     }
     catch (Exception ex)
     {
         DirectoryServicesUtils.rootDsePath = "RootDSE";
         new NetSqlAzMan.Logging.LoggingUtility().WriteError(null, "Cannot find RootDSE path. LDAP Queries should be fails !\r\n" + ex.Message);
     }
 }
 /// <summary>
 /// Executes the LDAP query.
 /// </summary>
 /// <param name="lDapQuery">The l dap query.</param>
 /// <returns></returns>
 public static bool TestLDAPQuery(string lDapQuery)
 {
     try
     {
         if (String.IsNullOrEmpty(lDapQuery))
         {
             return(true);
         }
         if (String.IsNullOrEmpty(lDapQuery.Trim()))
         {
             return(true);
         }
         DirectoryEntry root = DirectoryServicesUtils.newDirectoryEntry("LDAP://" + (DirectoryServicesUtils.GetRootDSEPart(lDapQuery) ?? SqlAzManStorage.RootDSEPath));
         root.RefreshCache();
         DirectorySearcher searcher = new DirectorySearcher(root, DirectoryServicesUtils.GetLDAPQueryPart(lDapQuery), new string[] { "objectSid" });
         searcher.FindOne();
         return(true);
     }
     catch
     {
         return(false);
     }
 }