public void TestSchemaClass()
        {
            using (ActiveDirectorySchemaClass orgClass = ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "organization"))
            {
                Assert.Equal("organization", orgClass.Name);
                Assert.Equal("Organization", orgClass.CommonName);
                Assert.Equal("2.5.6.4", orgClass.Oid);
                Assert.Equal("bf967aa3-0de6-11d0-a285-00aa003049e2", orgClass.SchemaGuid.ToString());
                Assert.Equal("top", orgClass.SubClassOf.Name);
                Assert.NotNull(orgClass.DefaultObjectSecurityDescriptor);
                string s = orgClass.Description; // it can be null
                Assert.False(orgClass.IsDefunct);

                Assert.True(orgClass.AuxiliaryClasses.Contains(ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "samDomainBase")));
                Assert.True(orgClass.PossibleInferiors.Contains(ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "user")));

                ActiveDirectorySchemaClass country = ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "country");
                Assert.True(orgClass.PossibleSuperiors.Contains(country));
                int index = orgClass.PossibleSuperiors.IndexOf(country);
                Assert.Equal(country.Name, orgClass.PossibleSuperiors[index].Name);

                Assert.True(orgClass.MandatoryProperties.Contains(ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "ntSecurityDescriptor")));
                Assert.True(orgClass.OptionalProperties.Contains(ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "description")));
                Assert.True(orgClass.MandatoryProperties.Contains(ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "objectClass")));

                using (DirectoryEntry de = orgClass.GetDirectoryEntry())
                {
                    Assert.True("CN=Organization".Equals(de.Name, StringComparison.OrdinalIgnoreCase));
                }
            }
        }
Example #2
0
        public static void CreateNewClass()
        {
            // create a new class object
            ActiveDirectorySchemaClass newClass =
                new ActiveDirectorySchemaClass(
                    context,
                    newClassLdapDisplayName);

            // set the properties for this schema class
            newClass.CommonName = newClassCommonName;
            newClass.Oid        = newClassOid;
            newClass.SubClassOf = ActiveDirectorySchemaClass.FindByName(
                context,
                newClassSubClassOf);
            newClass.Type = SchemaClassType.Structural;
            // adding the previously created property as an optional property
            newClass.OptionalProperties.Add(
                ActiveDirectorySchemaProperty.FindByName(
                    context,
                    newClassOptionalProperty));

            // save the new class to the backend store
            try
            {
                newClass.Save();
            }
            catch (ActiveDirectoryObjectExistsException)
            {
                // a class by this name already exists
                Console.WriteLine("FAILED to created new class.");
                return;
            }
            Console.WriteLine("Class \"{0}\" created successfully.",
                              newClassLdapDisplayName);
        }
Example #3
0
        public static void SchemaPropertySample()
        {
            Console.WriteLine();
            Console.WriteLine("<---------SCHEMA PROPERTY Sample---------->\n");

            // Find the schema property in the current forest
            // and print it's properties

            ActiveDirectorySchemaProperty schemaProperty;

            try
            {
                schemaProperty = ActiveDirectorySchemaProperty.FindByName(
                    context,
                    propertyName);
            }
            catch (ArgumentException e)
            {
                // this exception could be thrown if the current context
                // is not associated any domain/forest
                Console.WriteLine(e.Message);
                return;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                Console.WriteLine("Schema property \"{0}\" could not be found");
                return;
            }

            Console.WriteLine("Name: {0}", schemaProperty.Name);
            Console.WriteLine("Oid: {0}", schemaProperty.Oid);
            Console.WriteLine("Description: {0}", schemaProperty.Description);
            Console.WriteLine();

            // limits on the length of the property (in this case this is length
            // these could also mean upper and lower limits
            // values for value the property)
            try
            {
                Console.WriteLine("RangeLower: {0}", schemaProperty.RangeLower);
                Console.WriteLine("RangeUpper: {0}", schemaProperty.RangeUpper);
                Console.WriteLine();
            }
            catch (ArgumentNullException)
            {
                // if these properties are not defined for the schema property,
                // ArgumentNullException is thrown
                Console.WriteLine("RangeLower/RangeUpper not defined.");
            }

            // other characteristics of the property
            Console.WriteLine("IsIndexed: {0}", schemaProperty.IsIndexed);
            Console.WriteLine("IsInGlobalCatalog: {0}",
                              schemaProperty.IsInGlobalCatalog);
        }
        public static void GetSchemaPropertyData(string propertyName)
        {
            Console.WriteLine("<--SCHEMA ATTRIBUTE INFORMATION-->\n");

            // get a forest context
            DirectoryContext context = new DirectoryContext(
                DirectoryContextType.Forest);

            ActiveDirectorySchemaProperty schemaProperty;

            try
            {
                // bind to an attribute schema object
                schemaProperty = ActiveDirectorySchemaProperty.FindByName(
                    context,
                    propertyName);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                Console.WriteLine("Schema property \"{0}\" could not be found", propertyName);
                return;
            }

            Console.WriteLine("Name: {0}", schemaProperty.Name);
            Console.WriteLine("Oid: {0}", schemaProperty.Oid);
            Console.WriteLine("Description: {0}", schemaProperty.Description);

            // limits on the length of the property (in this case this is length
            // these could also mean upper and lower limits
            // values for the attribute)
            try
            {
                Console.WriteLine("RangeLower: {0}", schemaProperty.RangeLower);
                Console.WriteLine("RangeUpper: {0}", schemaProperty.RangeUpper);
            }
            catch (ArgumentNullException)
            {
                // if these properties are not defined for the schema property,
                // ArgumentNullException is thrown
                Console.WriteLine("RangeLower/RangeUpper not defined.");
            }

            // other characteristics of the property
            Console.WriteLine("IsIndexed: {0}", schemaProperty.IsIndexed);
            Console.WriteLine("IsInGlobalCatalog: {0}",
                              schemaProperty.IsInGlobalCatalog);
        }
Example #5
0
        public static void GetSchemaPropertyData(string propertyName)
        {
            Console.WriteLine("<--ADAM SCHEMA PROPERTY-->\n");

            ActiveDirectorySchemaProperty schemaProperty;

            try
            {
                // bind to an ADAM schema attribute object
                schemaProperty = ActiveDirectorySchemaProperty.FindByName(
                    adamContext,
                    propertyName);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                Console.WriteLine("Schema property \"{0}\" could not be found",
                                  propertyName);
                return;
            }

            Console.WriteLine("Name: {0}", schemaProperty.Name);
            Console.WriteLine("Oid: {0}", schemaProperty.Oid);
            Console.WriteLine("Description: {0}", schemaProperty.Description);
            Console.WriteLine("Schema GUID: {0}", schemaProperty.SchemaGuid);
            Console.WriteLine("IsIndexed: {0}", schemaProperty.IsIndexed);
            Console.WriteLine("IsSingleValued: {0}",
                              schemaProperty.IsSingleValued);

            Console.WriteLine("\nUpper and lower value/length:");
            try
            {
                Console.WriteLine("RangeLower: {0}", schemaProperty.RangeLower);
                Console.WriteLine("RangeUpper: {0}", schemaProperty.RangeUpper);
            }
            catch (ArgumentNullException)
            {
                // if these properties are not defined for the schema property,
                // ArgumentNullException is thrown
                Console.WriteLine("RangeLower/RangeUpper not defined.");
            }
        }
        public void TestSchema()
        {
            using (ActiveDirectorySchema schema = ActiveDirectorySchema.GetSchema(ActiveDirectoryContext))
            {
                Assert.True(schema.FindAllClasses().Contains(ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "user")));
                Assert.True(schema.FindAllClasses().Contains(ActiveDirectorySchemaClass.FindByName(ActiveDirectoryContext, "samDomainBase")));
                Assert.NotNull(schema.FindAllDefunctClasses());
                Assert.NotNull(schema.FindAllDefunctProperties());
                Assert.True(schema.FindAllProperties(PropertyTypes.Indexed).Contains(ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "ou")));
                Assert.True(schema.FindAllProperties().Contains(ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "cn")));
                Assert.Equal("person", schema.FindClass("person").Name);
                Assert.Equal("cn", schema.FindProperty("cn").Name);

                using (DirectoryEntry de = schema.GetDirectoryEntry())
                {
                    Assert.True("CN=Schema".Equals(de.Name, StringComparison.OrdinalIgnoreCase));
                }
            }
        }
        public void TestSchemaProperty()
        {
            using (ActiveDirectorySchemaProperty adsp = ActiveDirectorySchemaProperty.FindByName(ActiveDirectoryContext, "objectClass"))
            {
                Assert.Equal("Object-Class", adsp.CommonName);
                Assert.False(adsp.IsDefunct);
                Assert.False(adsp.IsInAnr);
                Assert.True(adsp.IsIndexed);
                Assert.False(adsp.IsIndexedOverContainer);
                Assert.True(adsp.IsInGlobalCatalog);
                Assert.True(adsp.IsOnTombstonedObject);
                Assert.False(adsp.IsSingleValued);
                Assert.False(adsp.IsTupleIndexed);
                Assert.Equal("2.5.4.0", adsp.Oid);

                using (DirectoryEntry de = adsp.GetDirectoryEntry())
                {
                    Assert.True("CN=Object-Class".Equals(de.Name, StringComparison.OrdinalIgnoreCase));
                }

                Assert.Equal(ActiveDirectorySyntax.Oid, adsp.Syntax);
            }
        }
Example #8
0
        public static void CreateNewClass()
        {
            // specify a common name
            string newClassCommonName = "new-Class";

            // specify an lDAPDisplayName
            string newClassLdapDisplayName = "newClass";

            // specify an OID value. The root name was generated by oidgen.exe
            string newClassOid =
                "1.2.840.113556.1.5.7000.111.28688.28684.8.240397.1734810.1181742.544876.1";

            string subClassOf = "top";

            string possibleSuperior = "organizationalUnit";

            // add an optional attribute to the new schema class object
            // This example adds the new attribute created in the CreateNewAttribute method
            string newClassOptionalAttribute = newAttributeLdapDisplayName;


            // create a new class object
            ActiveDirectorySchemaClass newClass =
                new ActiveDirectorySchemaClass(
                    adamContext,
                    newClassLdapDisplayName);

            // set the attribute values for this schema class object
            newClass.CommonName = newClassCommonName;
            newClass.Oid        = newClassOid;

            newClass.Type = SchemaClassType.Structural;

            // assign the parent class
            newClass.SubClassOf = ActiveDirectorySchemaClass.FindByName(adamContext,
                                                                        subClassOf);

            // add the previously created attribute as an optional attribute
            newClass.OptionalProperties.Add(
                ActiveDirectorySchemaProperty.FindByName(adamContext,
                                                         newClassOptionalAttribute));

            //add an OU as a possible superior so that this class can be
            //instantiated in an OU
            newClass.PossibleSuperiors.Add(
                ActiveDirectorySchemaClass.FindByName(adamContext,
                                                      possibleSuperior));

            // save the new class to the schema
            try
            {
                newClass.Save();
            }

            catch (ActiveDirectoryObjectExistsException e)
            {
                // an schema object by this name already exists in the schema
                Console.WriteLine("The schema object {0} was not created. {0}",
                                  newClassLdapDisplayName, e.Message);
                return;
            }

            catch (ActiveDirectoryOperationException e)
            {
                // a call to the underlying directory was rejected
                Console.WriteLine("The schema object {0} was not created. {0}",
                                  newClassLdapDisplayName, e.Message);
                return;
            }

            Console.WriteLine("Class \"{0}\" created successfully.",
                              newClassLdapDisplayName);
        }
Example #9
0
        private static void createSchemaClass(object domainDCs)
        {
            try
            {
                if (!DirectoryEntry.Exists("LDAP://CN=nmlsNetworkDriveClass,cn=Schema,cn=Configuration," + domainDCs))
                {
                    DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Forest);

                    ActiveDirectorySchemaClass schemaClass = new ActiveDirectorySchemaClass(ctx, "nmlsNetworkDriveClass");
                    schemaClass.Oid        = "1.2.840.113556.1.8000.2554.61823.22188.26389.18272.44952.1599828.8363415.3.1";
                    schemaClass.SubClassOf = new ActiveDirectorySchemaClass(ctx, "top");
                    schemaClass.OptionalProperties.AddRange(new ActiveDirectorySchemaProperty[] {
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveA"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveB"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveC"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveD"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveE"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveF"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveG"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveH"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveI"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveJ"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveK"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveL"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveM"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveN"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveO"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveP"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveQ"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveR"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveS"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveT"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveU"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveV"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveW"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveX"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveY"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveZ"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelA"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelB"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelC"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelD"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelE"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelF"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelG"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelH"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelI"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelJ"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelK"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelL"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelM"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelN"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelO"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelP"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelQ"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelR"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelS"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelT"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelU"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelV"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelW"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelX"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelY"),
                        ActiveDirectorySchemaProperty.FindByName(ctx, "nmlsNetworkDriveDelZ")
                    });

                    schemaClass.Type = System.DirectoryServices.ActiveDirectory.SchemaClassType.Auxiliary;

                    schemaClass.Save();

                    //schemaClass
                }
                else
                {
                    Console.WriteLine("Class already exists");
                }

                DirectoryEntry userClass = new DirectoryEntry("LDAP://cn=User,cn=Schema,cn=Configuration," + domainDCs);

                if (!userClass.Properties["auxiliaryClass"].Contains("nmlsNetworkDriveClass"))
                {
                    userClass.Properties["auxiliaryClass"].Add("nmlsNetworkDriveClass");
                    userClass.CommitChanges();
                }
                else
                {
                    Console.WriteLine("User class already has auxiliary class nmlsNetworkDriveClass");
                }


                // Add NMLS class also OU objects.

                DirectoryEntry ouClass = new DirectoryEntry("LDAP://cn=Organizational-Unit,cn=Schema,cn=Configuration," + domainDCs);
                if (!ouClass.Properties["auxiliaryClass"].Contains("nmlsNetworkDriveClass"))
                {
                    ouClass.Properties["auxiliaryClass"].Add("nmlsNetworkDriveClass");
                    ouClass.CommitChanges();
                }
                else
                {
                    Console.WriteLine("OU class already has auxiliary class nmlsNetworkDriveClass");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }