/// <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> /// Determines whether the specified login is group. /// </summary> /// <param name="login">The login.</param> /// <returns> /// <c>true</c> if the specified login is group; otherwise, <c>false</c>. /// </returns> public static Boolean IsGroup(String login) { NTAccount nta = new NTAccount(login); SecurityIdentifier sid = (SecurityIdentifier)nta.Translate(typeof(SecurityIdentifier)); Byte[] sidByte = new Byte[sid.BinaryLength]; sid.GetBinaryForm(sidByte, 0); return(DirectoryServicesUtils.IsGroup(sidByte)); }
/// <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; } }
/// <summary> /// Gets the member info. /// </summary> /// <param name="sid">The object owner.</param> /// <param name="memberName">Name of the member.</param> /// <param name="isAGroup">if set to <c>true</c> [is A group].</param> /// <param name="isLocal">if set to <c>true</c> [is local].</param> public static void GetMemberInfo(string sid, out string memberName, out bool isAGroup, out bool isLocal) { memberName = sid; isAGroup = true; isLocal = false; try { DirectoryServicesUtils.GetMemberInfo(sid, out memberName, out isLocal); isAGroup = DirectoryServicesUtils.IsGroup(DirectoryServicesUtils.ConvertStringToSid(sid)); } catch (Exception ex) { new NetSqlAzMan.Logging.LoggingUtility().WriteWarning(null, ex.Message + "\r\nSid: " + sid); } }
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); } }