private static IDirectoryAttributes AddEntryIfNecessary(string dn, string objectClass, IDirectoryContext context) { IDirectoryAttributes entry = null; try { entry = context.GetByDN(dn); } catch (DirectoryOperationException ex) { if (ex.Message.IndexOf("object does not exist", StringComparison.OrdinalIgnoreCase) == -1) { throw; } } if (entry == null) { entry = new DirectoryAttributes(dn); entry.Set("objectClass", objectClass); Console.WriteLine("Adding {0}", dn); return(context.AddAndGet(entry)); } Console.WriteLine("{0} already exists", dn); return(entry); }
/// <summary> /// Adds the entry to the directory and returns the newly saved entry from the directory. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="entry">The entry to add.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <param name="listeners">The event listeners to be notified.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> or <paramref name="entry"/>> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the add was not successful.</exception> /// <exception cref="LdapException">Thrown if the operation fails.</exception> public static IDirectoryAttributes AddAndGet(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IAddEventListener> listeners = null) { Add(connection, entry, log, controls, listeners); return(GetByDN(connection, entry.DistinguishedName, log)); }
private static void PopulateDirectoryForTests() { var user2Container = DnParser.ParseName(TestUserDirectoryContainer); var user2Prefix = DnParser.ParseRDN(TestUserDirectoryContainer); AddContainerIfNecessary(user2Prefix, user2Container); using (var context = new DirectoryContext()) { AddEntryIfNecessary("CN=PasswordUser," + TestUserDirectoryContainer, "user", context); AddEntryIfNecessary("CN=persontest," + TestUserDirectoryContainer, "user", context); AddEntryIfNecessary("CN=TestUser," + TestUserDirectoryContainer, "user", context); AddEntryIfNecessary("CN=TestUser2," + TestUserDirectoryContainer, "user", context); } var roleContainer = DnParser.ParseName(RolesDirectoryContainer); var rolePrefix = DnParser.ParseRDN(RolesDirectoryContainer); AddContainerIfNecessary(rolePrefix, roleContainer); using (var context = new DirectoryContext()) { IDirectoryAttributes rangeTest = AddEntryIfNecessary("CN=RangeTest," + RolesDirectoryContainer, "group", context); foreach (var kvp in rangeTest.Where(kvp => kvp.Key.StartsWith("member", StringComparison.OrdinalIgnoreCase))) { if (kvp.Value is IEnumerable <string> && ((IEnumerable <string>)kvp.Value).Any()) { Console.WriteLine("RangeTest members already populated"); return; } } var newMembers = new List <string>(); newMembers.AddRange(context.Query <LdsUser>().Take(10000).Select(u => u.DistinguishedName)); rangeTest.Set("member", newMembers); context.Update(rangeTest); } }
protected ConnectionHolder GetConnectionFor(string userName, string password, IDirectoryAttributes userEntry = null, AuthType authType = AuthType.Basic) { if (userEntry == null) using (var holder = GetConnectionForServiceUser()) { userEntry = LookupUserEntry(holder, userName); if (userEntry == null) throw new ApplicationException(string.Format("No object was found with the user name {0}.", userName)); } return GetConnectionForCore(new NetworkCredential(userEntry.DistinguishedName, password), authType); }
public void SetUp() { _guidBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; _siBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; var strings = new[] { "one", "two", "three", "four" }; _byteArrays = new[] { _guidBytes, _siBytes }; _collection = typeof(SearchResultAttributeCollection).Create <SearchResultAttributeCollection>(); _collection .Call("Add", new object[] { "Property1", new DirectoryAttribute("Property1", "prop1") }); _collection .Call("Add", new object[] { "Property2", new DirectoryAttribute("Property2", "2") }); _collection .Call("Add", new object[] { "Property3", new DirectoryAttribute("Property3", _guidBytes) }); _collection .Call("Add", new object[] { "Property4", new DirectoryAttribute("Property4", strings) }); _collection .Call("Add", new object[] { "Property5", new DirectoryAttribute("Property5", _siBytes) }); _collection .Call("Add", new object[] { "Property6", new DirectoryAttribute("Property6", "TRUE") }); _collection .Call("Add", new object[] { "Property7", new DirectoryAttribute("Property7", "20110313064859.0Z") }); _collection .Call("Add", new object[] { "Property8", new DirectoryAttribute("Property8", "129444725394225946") }); _collection .Call("Add", new object[] { "Property9", new DirectoryAttribute("Property9", "20110313064859Z") }); _collection .Call("Add", new object[] { "Property10", new DirectoryAttribute("Property10", _byteArrays) }); _collection .Call("Add", new object[] { "Property11", new DirectoryAttribute("Property10", Resources.ResourceHelper.GetAssemblyResource(@"Resources\cert.cer")) }); _entry = typeof(SearchResultEntry).Create <SearchResultEntry>( new object[] { "theDn", _collection }); _attributes = new DirectoryAttributes(_entry); }
/// <summary> /// Adds the entry to the directory. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="entry">The entry to add.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <param name="listeners">The event listeners to be notified.</param> /// <param name="resultProcessing">How the async results are processed</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> or <paramref name="entry"/>> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the add was not successful.</exception> /// <exception cref="LdapException">Thrown if the operation fails.</exception> public static async Task AddAsync(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IAddEventListener> listeners = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing) { string distinguishedName = null; try { if (connection == null) { throw new ArgumentNullException("connection"); } if (entry == null) { throw new ArgumentNullException("entry"); } distinguishedName = entry.DistinguishedName; if (distinguishedName.IsNullOrEmpty()) { throw new ArgumentException("entry.DistinguishedName is invalid."); } var request = new AddRequest(distinguishedName, entry.GetChangedAttributes().Where(da => da.Count > 0).ToArray()); if (controls != null) { request.Controls.AddRange(controls); } if (listeners != null) { var args = new ListenerPreArgs <object, AddRequest>(entry, request, connection); foreach (var eventListener in listeners.OfType <IPreAddEventListener>()) { eventListener.Notify(args); } } if (log != null && log.TraceEnabled) { log.Trace(request.ToLogString()); } AddResponse response = null; #if NET45 await Task.Factory.FromAsync( (callback, state) => { return(connection.BeginSendRequest(request, resultProcessing, callback, state)); }, (asyncresult) => { response = (AddResponse)connection.EndSendRequest(asyncresult); response.AssertSuccess(); }, null ).ConfigureAwait(false); #else response = await Task.Run(() => connection.SendRequest(request) as AddResponse).ConfigureAwait(false); response.AssertSuccess(); #endif if (listeners != null) { var args = new ListenerPostArgs <object, AddRequest, AddResponse>(entry, request, response, connection); foreach (var eventListener in listeners.OfType <IPostAddEventListener>()) { eventListener.Notify(args); } } } catch (Exception ex) { if (log != null) { log.Error(ex, string.Format("An error occurred while trying to add '{0}'.", distinguishedName)); } throw; } }
/// <summary> /// Updates the entry in the directory and returns the updated version from the directory. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="entry">The entry to update.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <param name="listeners">The event listeners to be notified.</param> /// <param name="resultProcessing">How the async results are processed</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception> /// <exception cref="LdapException">Thrown if the operation fails</exception> public static async Task <IDirectoryAttributes> UpdateAndGetAsync(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IUpdateEventListener> listeners = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing) { await UpdateAsync(connection, entry, log, controls, listeners, resultProcessing).ConfigureAwait(false); return(await GetByDNAsync(connection, entry.DistinguishedName, log, null, resultProcessing).ConfigureAwait(false)); }
/// <summary> /// Adds the entry to the directory. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="entry">The entry to add.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <param name="listeners">The event listeners to be notified.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> or <paramref name="entry"/>> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the add was not successful.</exception> /// <exception cref="LdapException">Thrown if the operation fails.</exception> public static void Add(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IAddEventListener> listeners = null) { string distinguishedName = null; try { if (connection == null) { throw new ArgumentNullException("connection"); } if (entry == null) { throw new ArgumentNullException("entry"); } distinguishedName = entry.DistinguishedName; if (distinguishedName.IsNullOrEmpty()) { throw new ArgumentException("entry.DistinguishedName is invalid."); } var request = new AddRequest(distinguishedName, entry.GetChangedAttributes().Where(da => da.Count > 0).ToArray()); if (controls != null) { request.Controls.AddRange(controls); } if (listeners != null) { var args = new ListenerPreArgs <object, AddRequest>(entry, request, connection); foreach (var eventListener in listeners.OfType <IPreAddEventListener>()) { eventListener.Notify(args); } } if (log != null && log.TraceEnabled) { log.Trace(request.ToLogString()); } var response = connection.SendRequest(request) as AddResponse; response.AssertSuccess(); if (listeners != null) { var args = new ListenerPostArgs <object, AddRequest, AddResponse>(entry, request, response, connection); foreach (var eventListener in listeners.OfType <IPostAddEventListener>()) { eventListener.Notify(args); } } } catch (Exception ex) { if (log != null) { log.Error(ex, string.Format("An error occurred while trying to add '{0}'.", distinguishedName)); } throw; } }
/// <summary> /// Executes <see cref="DirectoryContext.Add(IDirectoryAttributes)"/> /// </summary> /// <param name="context">The <see cref="DirectoryContext"/>.</param> /// <param name="entry">The attributes for the entry</param> public static Task AddAsync(this IDirectoryContext context, IDirectoryAttributes entry) { return(Task.Factory.StartNew(() => context.Add(entry))); }
/// <summary> /// Executes <see cref="DirectoryContext.Update(IDirectoryAttributes,DirectoryControl[])"/> within a <see cref="Task"/>. /// </summary> /// <param name="context">The context.</param> /// <param name="entry">The attributes for the entry.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception> /// <exception cref="LdapException">Thrown if the operation fails</exception> public static Task UpdateAsync(this IDirectoryContext context, IDirectoryAttributes entry, DirectoryControl[] controls) { return(Task.Factory.StartNew(() => context.Update(entry, controls))); }
/// <summary> /// Executes <see cref="DirectoryContext.UpdateAndGet(IDirectoryAttributes)"/> within a <see cref="Task"/>. /// </summary> /// <param name="context">The context.</param> /// <param name="entry">The attributes for the entry.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception> /// <exception cref="LdapException">Thrown if the operation fails</exception> public static Task <IDirectoryAttributes> UpdateAndGetAsync(this IDirectoryContext context, IDirectoryAttributes entry) { return(Task.Factory.StartNew(() => context.UpdateAndGet(entry))); }
/// <summary> /// Executes <see cref="DirectoryContext.AddAndGet(IDirectoryAttributes,DirectoryControl[])"/> /// </summary> /// <param name="context">The <see cref="DirectoryContext"/>.</param> /// <param name="entry">The attributes for the entry</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> public static Task <IDirectoryAttributes> AddAndGetAsync(this IDirectoryContext context, IDirectoryAttributes entry, DirectoryControl[] controls) { return(Task.Factory.StartNew(() => context.AddAndGet(entry, controls))); }
protected virtual CreateUserParams CreateUserParams(string userName, string password, IDirectoryAttributes entry) { string email = null; //if (!entry.AttributeNames.Contains(Directory.UserEmailAttribute)) // throw new ApplicationException(string.Format("Attribute {0} was not found on the object.", Directory.UserEmailAttribute)); //var email = entry.GetString(Directory.UserEmailAttribute); //if (string.IsNullOrEmpty(email)) // throw new ApplicationException(string.Format("Attribute {0} is not set on the object.", Directory.UserEmailAttribute)); if (entry.AttributeNames.Contains(Directory.UserEmailAttribute)) { email = entry.GetString(Directory.UserEmailAttribute); } return new CreateUserParams(userName, password, email, null, null, true); }
/// <summary> /// Executes <see cref="LdapConnectionExtensions.UpdateAndGet"/> within a <see cref="Task"/>. /// </summary> /// <param name="connection">The connection to the directory.</param> /// <param name="entry">The entry to update.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> /// <param name="listeners">The event listeners to be notified.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null. /// </exception> /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception> /// <exception cref="LdapException">Thrown if the operation fails</exception> public static Task <IDirectoryAttributes> UpdateAndGetAsync(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IPreUpdateEventListener> listeners = null) { return(Task.Factory.StartNew(() => connection.UpdateAndGet(entry, log, controls, listeners))); }