How to get current domain name pragmatically

Sometimes, you need to get the current domain name you are running on, for example to append it to a user name.

Environment.UserDomainName

Advertisements

Nested Group membership in Active Directory

Sometimes when you first install Windows server 2003 and active directory, you are unable to add a group to be a member of another group. In order to solve that problem just raise the domain functional level to Windows server 2003.

1- Right click the domain root node from the management console.

2- select “Raise Domain functional Level …

3- Choose Windows server 2003 under the current domain functional level.

How to get all users Active Directory Schema properties

I encountered a problem where I wanted to retrieve all the active directory user’s attributes, configured and not configured, in a Hashtable and a flag whether the attribute is multivalue or not. I tried first

DirectoryEntry _entry = new DirectoryEntry(path);
return _entry.Properties;

The problem with that is that it returns only the configured attributes. After searching for a while I reached that solution which returns all the attributes for an active directory entry, configured and not configured.

public static Hashtable GetADSchemaProperties()
{
   //connect to AD and get the current schema.
   ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();
   DirectoryEntry user = null;
   try
   {
     //get the current username
     WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
     WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);
     string userName = currentPrincipal.Identity.Name;
     userName = userName.Split(new char[] { '\\' })[1];

        //get the current user
        DirectoryEntry user = GetADEntryUser(userName);

        ArrayList properties = FindUserProperties(user);
        if (properties == null || properties.Count == 0)
            return null;

       //Add values to Hashtable.
       Hashtable propertiesList = new Hashtable();
       foreach (string propertyName in properties)
               propertiesList.Add(propertyName, !schema.FindPropert(propertyName).IsSingleValued);
       return propertiesList;
}
catch (Exception ex)
{
    Logging.GetInstance().LogException(ex);
    return null;
}
finally
{
    //Dispose all objects
    if(user != null)
        user.Dispose();
    if(schema != null)
        schema.Dispose();
}
}

private static ArrayList FindUserProperties(DirectoryEntry user)
{
   string ldapPath = user.LDAPPath.ToString();
   DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath);
   directoryEntry.AuthenticationType = 
   AuthenticationTypes.Secure|AuthenticationTypes.ServerBind | AuthenticationTypes.Sealing;
   object o = directoryEntry.NativeObject;

   DirectorySearcher mySearcher = new DirectorySearcher(directoryEntry);
   mySearcher.Filter = (“(objectClass=user)”);
   mySearcher.PropertyNamesOnly = true;
   mySearcher.PropertiesToLoad.Clear();

   SearchResult searchRes = mySearcher.FindOne();
   if (searchRes == null)
      return new ArrayList();

   DirectoryEntry schemaEntry = new DirectoryEntry((searchRes.GetDirectoryEntry().SchemaEntry.Path));

   //NOTE: One place where managed ADSI (System.DirectoryServices) falls short is finding schema
   //information from LDAP/AD objects. Finding information like mandatory and optional
   //properties simply cannot be done with any managed classes

   IADsClass iadsClass = (IADsClass)schemaEntry.NativeObject;
   if (iadsClass == null)
   return new ArrayList();

   ArrayList list = new ArrayList();
   foreach (string s in (Array)iadsClass.OptionalProperties)
   {
       list.Add(s);
   }
   foreach (string s in (Array)iadsClass.MandatoryProperties)
   {
      list.Add(s);
   }
   return list;
}

Retrieving all Active Directory schema properties

Sometimes, you need to retrieve all current Active Directory schema properties.


//connect to AD and get the schema properties.
ActiveDirectorySchema schema=ActiveDirectorySchema.GetCurrentSchema();
ReadOnlyActiveDirectorySchemaPropertyCollection properties = schema.FindAllProperties();

Properties will contain a collection of all the current AD properties. you can check whatever properties’ values. For example, properties[0].Name, properties[0].IsSingleValued