public static SearchResultCollection Search(DirectoryEntry searchRoot, string filter, bool novellSupport, string guidProp) { var dsDirSearcher = new DirectorySearcher(searchRoot) { Filter = filter, SizeLimit = 10000, PageSize = 10000 }; // Set the search filter // NOVELL - force searcher to retrieve the objects' GUID // - this is not done by default when connecting to Novell eDirectory if (novellSupport) { dsDirSearcher.PropertiesToLoad.Add(guidProp); } try { return(dsDirSearcher.FindAll()); } catch (Exception e) { AdLog.LogException(e); } return(null); }
// ============================================================================== Static methods private static async Task <SyncConfiguration> LoadConfiguration() { try { dynamic settingsContent = Content.LoadAsync(SettingsPath).Result; if (settingsContent == null) { return(null); } string binaryUrl = _siteUrl.TrimEnd('/') + settingsContent.Binary.__mediaresource.media_src + "&includepasswords=true"; var settingsText = await RESTCaller.GetResponseStringAsync(new Uri(binaryUrl)); var config = JsonHelper.Deserialize <SyncConfiguration>(settingsText); // decrypt passwords and inject them back to the configuration foreach (var server in config.Servers.Where(server => server.LogonCredentials != null && !string.IsNullOrEmpty(server.LogonCredentials.Password))) { var request = new ODataRequest { ActionName = "Decrypt", Path = "Root", IsCollectionRequest = false, SiteUrl = _siteUrl }; try { server.LogonCredentials.Password = await RESTCaller.GetResponseStringAsync( request.GetUri(), ClientContext.Current.Servers[0], HttpMethod.Post, JsonHelper.Serialize(new { text = server.LogonCredentials.Password })); } catch (ClientException cex) { AdLog.LogError("Error during password decryption. " + Common.FormatClientException(cex)); } catch (Exception ex) { AdLog.LogException(ex); } } // preload all AD-related content types from the server ADRelatedContentTypes = await LoadADRelatedContentTypes(); return(config); } catch (Exception ex) { AdLog.LogException(ex); } return(null); }
public static async Task <IEnumerable <Content> > QueryContentByTypeAndPath(ADObjectType objType, string startPath, string[] select = null, int skip = 0, int top = 0) { try { var types = GetContentTypeNames(objType); QuerySettings settings = null; if (skip > 0 || top > 0) { settings = new QuerySettings { Skip = skip, Top = top }; } return(await Content.QueryForAdminAsync("+InTree:'" + startPath + "' +TypeIs:(" + string.Join(" ", types) + ") .SORT:Path", select, settings : settings)); } catch (Exception ex) { AdLog.LogException(ex); } return(new Content[0]); }
/* ==================================================================================== Static Methods */ public static DirectoryEntry ConnectToAD(string ldapPath, Server server) { var deADConn = new DirectoryEntry(ldapPath); var credentials = server.LogonCredentials; if (credentials != null) { if (credentials.Anonymous) { deADConn.AuthenticationType = AuthenticationTypes.Anonymous; } else if (!string.IsNullOrEmpty(credentials.Username)) { deADConn.AuthenticationType |= AuthenticationTypes.ServerBind; deADConn.Username = credentials.Username; deADConn.Password = credentials.Password; } } else { deADConn.AuthenticationType = AuthenticationTypes.Anonymous; } if (server.UseSsl) { deADConn.AuthenticationType |= AuthenticationTypes.SecureSocketsLayer; } //TODO: authentication: use SASL Exception exADConnectException = null; var bError = false; for (var i = 0; i < 3; i++) { try { var oNativeObject = deADConn.NativeObject; bError = false; break; } catch (Exception ex) { bError = true; exADConnectException = ex; System.Threading.Thread.Sleep(3000); } } if (bError) { AdLog.LogException(exADConnectException); throw new Exception("Connecting to AD server failed", exADConnectException); } // NOVELL - use a searcher to retrieve the objects' GUID // - directoryentry properties does not include guid when connecting to Novell eDirectory if (server.Novell) { var dsDirSearcher = new DirectorySearcher(deADConn); dsDirSearcher.PropertiesToLoad.Add(server.GuidProperty); dsDirSearcher.SearchScope = SearchScope.Base; var result = dsDirSearcher.FindOne(); var guid = result.Properties[server.GuidProperty][0]; deADConn.Properties[server.GuidProperty].Add(guid); } return(deADConn); }