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); }
public void Can_Add_Update_Remove_Dynamic() { var attributes = new DirectoryAttributes("CN=IntegrationTest," + IntegrationUserTest.NamingContext); attributes.SetNull("AccountExpires"); attributes.Set("objectclass", "user"); var added = _context.AddAndGet(attributes); added.Should().Not.Be.Null(); added.GetString("cn").Should().Be.EqualTo("IntegrationTest"); added.GetString("accountexpires").Should().Be.Null(); added.GetGuid("objectguid").Should().Have.Value(); added.GetSecurityIdentifier("objectsid").Should().Not.Be.Null(); added.GetSecurityIdentifiers("objectsid").Should().Not.Be.Empty(); added.GetStrings("objectclass").Should().Have.Count.GreaterThan(1); added.Set("accountExpires", "9223372036854775807").SetNull("manager"); added = _context.UpdateAndGet(added); added.GetString("accountExpires").Should().Be.EqualTo("9223372036854775807"); added.GetDateTime("accountExpires", null).Should().Not.Have.Value(); added.GetString("manager").Should().Be.Null(); var renamed = _context.RenameEntry(added.DistinguishedName, "IntegrationTest2"); var moved = _context.MoveEntry(renamed, IntegrationUserTest.NamingContext2); _context.Delete(moved); Executing.This(() => _context.GetByDN(moved)) .Should().Throw <DirectoryOperationException>().And.Exception.Message .Should().Contain("does not exist"); }
/// <summary> /// Executes <see cref="DirectoryContext.AddAndGet(DirectoryAttributes)"/> within a <see cref="Task"/>. /// </summary> /// <param name="context">The <see cref="DirectoryContext"/>.</param> /// <param name="entry">The attributes for the entry</param> public static Task <IDirectoryAttributes> AddAndGetAsync(this IDirectoryContext context, DirectoryAttributes entry) { return(Task.Factory.StartNew(() => context.AddAndGet(entry))); }
/// <summary> /// Executes <see cref="DirectoryContext.AddAndGet{T}(T,string,DirectoryControl[])"/> /// </summary> /// <typeparam name="T">The type of entry.</typeparam> /// <param name="context">The <see cref="DirectoryContext"/>.</param> /// <param name="entry">The object to save.</param> /// <param name="distinguishedName">The distinguished name for the entry.</param> /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param> public static Task <T> AddAndGetAsync <T>(this IDirectoryContext context, T entry, string distinguishedName = null, DirectoryControl[] controls = null) where T : class { return(Task.Factory.StartNew(() => context.AddAndGet(entry, distinguishedName, controls))); }