コード例 #1
0
ファイル: Program.cs プロジェクト: zeWizard/LinqToLdap
        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);
        }
コード例 #2
0
        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");
        }
コード例 #3
0
 /// <summary>
 /// Executes <see cref="DirectoryContext.GetByDN"/> within a <see cref="Task"/>.
 /// </summary>
 /// <param name="context">The <see cref="DirectoryContext"/>.</param>
 /// <param name="distinguishedName">The distinguished name to look for.</param>
 /// <param name="attributes">The attributes to load.</param>
 /// <returns></returns>
 public static Task <IDirectoryAttributes> GetByDNAsync(this IDirectoryContext context, string distinguishedName, params string[] attributes)
 {
     return(Task.Factory.StartNew(() => context.GetByDN(distinguishedName, attributes)));
 }
コード例 #4
0
 /// <summary>
 /// Executes <see cref="DirectoryContext.GetByDN{T}"/> within a <see cref="Task"/>.
 /// </summary>
 /// <param name="context">The <see cref="DirectoryContext"/>.</param>
 /// <param name="distinguishedName">The distinguished name to look for.</param>
 /// <typeparam name="T">The type of mapped object</typeparam>
 /// <returns></returns>
 public static Task <T> GetByDNAsync <T>(this IDirectoryContext context, string distinguishedName) where T : class
 {
     return(Task.Factory.StartNew(() => context.GetByDN <T>(distinguishedName)));
 }