public static IEnumerable <string> QueryDNsByInvokeGet(this DirectoryEntry source, string propertyName) { if (source == null) { throw new ArgumentNullException("source"); } var members = (IEnumerable)source.InvokeGet(propertyName); return(members == null ? null : members.Cast <object>() .Select(item => { var itemAsString = (item as string); if (itemAsString != null) { return itemAsString; } var itemAsAds = (item as UnsafeNativeMethods.IAds); if (itemAsAds != null) { return Ldap.GetDNFromADsPath(itemAsAds.ADsPath); } return null; }) .Where(x => x != null) .ToList()); }
public static string GetName(this SearchResult source) { if (source == null) { throw new ArgumentNullException("source"); } var split = source.Path.Split(_splitChars); return(Ldap.GetNameFromDN(split[split.Length - 1])); }
public static IEnumerable <T> QueryByIdentities <TSource, T>(this DirectoryEntry source, string identityProperty, string[] propertiesToLoad, IEnumerable <TSource> identities, Func <SearchResult, T> selector, QueryInfo queryInfo) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(identityProperty)) { throw new ArgumentNullException("identityProperty"); } if (propertiesToLoad == null || propertiesToLoad.Length == 0) { propertiesToLoad = new string[] { } } ; if (selector == null) { throw new ArgumentNullException("selector"); } if (queryInfo == null) { queryInfo = new QueryInfo(); } // var list = new List <T>(); foreach (var batch in identities.GroupAt(queryInfo.BatchSize)) { var filterPart = "(" + Ldap.EncodeFilter(identityProperty, true) + "=" + string.Join(")(" + Ldap.EncodeFilter(identityProperty, true) + "=", batch.Select(Ldap.EncodeFilter).ToArray()) + ")"; var directorySearcher = new DirectorySearcher(source, string.Format(queryInfo.QueryFilter, "(|" + filterPart + ")"), propertiesToLoad); directorySearcher.PageSize = 1000; if (queryInfo.Limiter != null) { queryInfo.Limiter(directorySearcher); } list.AddRange( directorySearcher.FindAll() .Cast <SearchResult>() .Select(selector) .OfType <T>()); } return(list); }
public static string MovePrincipal(this Func <string, DirectoryEntry> source, bool whatIf, string sourceDN, string targetDN) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(sourceDN)) { throw new ArgumentNullException("sourceDN"); } if (string.IsNullOrEmpty(targetDN)) { throw new ArgumentNullException("targetDN"); } Trace.TraceInformation("MovePrincipal: " + sourceDN + "->" + targetDN); if (whatIf) { return("R: " + sourceDN + "->" + targetDN + "\n"); } using (var sourceDE = source(sourceDN)) using (var targetDE = source(Ldap.GetContainerFromDN(targetDN))) sourceDE.MoveTo(targetDE, "CN=" + Ldap.GetNameFromDN(targetDN)); return(null); }