/// <summary> /// Retrieves the attributes from the directory using the distinguished name. <see cref="SearchScope.Base"/> is used. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="distinguishedName">The distinguished name to look for.</param> /// <param name="attributes">The attributes to load.</param> /// <returns></returns> public static IDirectoryAttributes GetByDN(this LdapConnection connection, string distinguishedName, ILinqToLdapLogger log = null, params string[] attributes) { try { if (connection == null) { throw new ArgumentNullException("connection"); } var request = new SearchRequest { DistinguishedName = distinguishedName, Scope = SearchScope.Base }; if (attributes != null) { request.Attributes.AddRange(attributes); } var transformer = new DynamicResultTransformer(); if (log != null && log.TraceEnabled) { log.Trace(request.ToLogString()); } var response = connection.SendRequest(request) as SearchResponse; response.AssertSuccess(); // ReSharper disable PossibleNullReferenceException return((response.Entries.Count == 0 // ReSharper restore PossibleNullReferenceException ? transformer.Default() : transformer.Transform(response.Entries[0])) as IDirectoryAttributes); } catch (Exception ex) { if (log != null) { log.Error(ex, string.Format("An error occurred while trying to retrieve '{0}'.", distinguishedName)); } throw; } }
/// <summary> /// Retrieves the attributes from the directory using the distinguished name. <see cref="SearchScope.Base"/> is used. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="distinguishedName">The distinguished name to look for.</param> /// <param name="attributes">The attributes to load.</param> /// <param name="resultProcessing">How the async results are processed</param> /// <returns></returns> public static async Task <IDirectoryAttributes> GetByDNAsync(this LdapConnection connection, string distinguishedName, ILinqToLdapLogger log = null, string[] attributes = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing) { try { if (connection == null) { throw new ArgumentNullException("connection"); } var request = new SearchRequest { DistinguishedName = distinguishedName, Scope = SearchScope.Base }; if (attributes != null) { request.Attributes.AddRange(attributes); } var transformer = new DynamicResultTransformer(); if (log != null && log.TraceEnabled) { log.Trace(request.ToLogString()); } #if NET45 return(await System.Threading.Tasks.Task.Factory.FromAsync( (callback, state) => { return connection.BeginSendRequest(request, resultProcessing, callback, state); }, (asyncresult) => { var response = connection.EndSendRequest(asyncresult) as SearchResponse; response.AssertSuccess(); return (response.Entries.Count == 0 ? transformer.Default() : transformer.Transform(response.Entries[0])) as IDirectoryAttributes; }, null ).ConfigureAwait(false)); #else var response = await Task.Run(() => connection.SendRequest(request) as SearchResponse).ConfigureAwait(false); response.AssertSuccess(); return((response.Entries.Count == 0 ? transformer.Default() : transformer.Transform(response.Entries[0])) as IDirectoryAttributes); #endif } catch (Exception ex) { if (log != null) { log.Error(ex, string.Format("An error occurred while trying to retrieve '{0}'.", distinguishedName)); } throw; } }