Property Types 

There are several types of properties that are used with directory objects. In the Active Directory schema, these property types are called attribute syntaxes. For more information about attribute syntaxes, and for a list of attribute syntaxes that can be used in Active Directory, see the Syntaxes for Active Directory Attributes topic in the MSDN library at https://msdn.microsoft.com/libraryadschema.syntaxes.

The following topics provide code examples that show how to read and write property types using System.DirectoryServices:

Interpreted Data Types

There are two different ways to retrieve property values from System.DirectoryServices namespace. The first is to use the members of the Properties property. The other is to use the members of the ResultPropertyValueCollection collection that are obtained with the DirectorySearcher class. Each of these will return generic objects whose actual data type depends on the schema data type of the property. The Properties property will return the same object type as the IADs.GetInfoEx method. (For more information about the IADs.GetInfoEx method, see the IADs::GetInfoEx topic in the MSDN Library at https://msdn.microsoft.com/library.) The Item property will translate some of the data types into .NET Framework data types. The following table shows the Active Directory schema types and their associated interpreted and un-interpreted data types. For more information about an Active Directory Schema Type or COM interface name listed in the following table, see the topic for that specific type or COM interface name in the MSDN Library at https://msdn.microsoft.com/library.

Active Directory Schema Type Un-interpreted Type (as returned by Properties) Interpreted Type (as returned by ResultPropertyValueCollection)

Boolean

Boolean

Boolean

Enumeration

Int32

Int32

Enumeration (Delivery-Mechanism)

Int32

Int32

Enumeration (Export-Information-Level)

Int32

Int32

Enumeration (Preferred-Delivery-Method)

Int32

Int32

Integer

Int32

Int32

Interval

A COM object that can be cast to an IADsLargeInteger.

Int64

LargeInteger

A COM object that can be cast to an IADsLargeInteger.

Int64

Object(Access-Point)

Not supported

Not supported

Object(DN-Binary)

A COM object that can be cast to an IADsDNWithBinary.

A String that contains the distinguished name and binary data in the format that is specified by Object(DN-Binary).

Object(DN-String)

A COM object that can be cast to an IADsDNWithString.

A String that contains the distinguished name and string data in the format that is specified by Object(DN-String).

Object(DS-DN)

String

String

Object(OR-Name)

A COM object that can be cast to an IADsDNWithBinary.

A String that contains the distinguished name and binary data in the format that is specified by Object(DN-Binary).

Object(Presentation-Address)

String

String

Object(Replica-Link)

Byte[]

Byte[]

String(Generalized-Time)

DateTime

DateTime

String(IA5)

String

String

String(NT-Sec-Desc)

A COM object that can be cast to an IADsSecurityDescriptor.

Byte[]

String(Numeric)

String

String

String(Object-Identifier)

String

String

String(Octet)

Byte[]

Byte[]

String(Printable)

String

String

String(Sid)

Byte[]

Byte[]

String(Teletex)

String

String

String(Unicode)

String

String

String(UTC-Time)

DateTime

T:System.DateTime

Interpreting ADSI Object Property Values

For some Active Directory syntax types, such as LargeIntegeradschema.s_largeinteger, System.DirectoryServices will return the property value as a COM object. This COM object must be cast to the appropriate ADSI type to obtain the actual property type. For example, the lastLogon property belongs to the Interval syntax. System.DirectoryServices will return the property value for an Interval syntax as a COM object that supports the IADsLargeInteger interface. For more information about these elements, see the LargeInteger topic, the Interval topic, the IADsLargeInteger topic, and the lastLogon topic in the MSDN Library at https://msdn.microsoft.com/library.

The following C# example shows how to get the IADsLargeInteger interface from the COM object by casting the COM object to an ActiveDs.IADsLargeInteger object. If you are developing an application that uses objects in the ActiveDS namespace, reference the ActiveDS Type Library, activeds.tlb, when compiling and linking the application.

object obj = entry.Properties["lastLogon"];
ActiveDs.IADsLargeInteger largeIntADSI;
largeIntADSI = (ActiveDs.IADsLargeInteger)obj;

The following table lists the syntax types that System.DirectoryServices will return as COM objects and the associated ADSI interfaces of each syntax type. For information about a syntax type or ADSI interface listed in the following table, see the topic for that specific syntax type or ADSI interface in the MSDN Library at https://msdn.microsoft.com/library.

Syntax Type ADSI Interface

Interval

IADsLargeInteger

LargeInteger

IADsLargeInteger

Object(DN-Binary)

IADsDNWithBinary

Object(DN-String)

IADsDNWithString

Object(OR-Name)

IADsDNWithBinary

String(NT-Sec-Desc)

IADsSecurityDescriptor

The following C# example shows how to obtain the appropriate interface for an ADSI COM object. This example uses the InvalidCastException exception to determine if the cast is valid.

static string GetADSIComObjectValue(object obj)
{
    if(obj.GetType().Name.Equals("__ComObject"))
    {
        /*
        Try IADsSecurityDescriptor. This is returned for the following AD 
        syntax type:

        String(NT-Sec-Desc) 
        */
        try
        {
            ActiveDs.IADsSecurityDescriptor secDesc;
            secDesc = (ActiveDs.IADsSecurityDescriptor)obj;
            return "IADsSecurityDescriptor:" + secDesc.Owner.ToString();
        }
        catch (System.InvalidCastException)
        {
        }

        /*
        Try IADsLargeInteger. This is returned for the following AD syntax 
        types:
        
        Interval
        LargeInteger
        */
        try
        {
            ActiveDs.IADsLargeInteger largeIntADSI;
            largeIntADSI = (ActiveDs.IADsLargeInteger)obj;
            Int64 largeInt = largeIntADSI.HighPart * 0x100000000;
            largeInt += largeIntADSI.LowPart;
            return "IADsLargeInteger:" + largeInt.ToString();
        }
        catch (System.InvalidCastException)
        {
        }

        /*
        Try IADsDNWithBinary. This is returned for the following AD syntax 
        types:
        
        Object(DN-Binary)
        Object(OR-Name)
        */
        try
        {
            ActiveDs.IADsDNWithBinary dnWithBinary;
            dnWithBinary = (ActiveDs.IADsDNWithBinary)obj;
            return "IADsDNWithBinary:" + 
                dnWithBinary.DNString + ":" + 
                dnWithBinary.BinaryValue.ToString();
        }
        catch (System.InvalidCastException)
        {
        }

        /*
        Try IADsDNWithString. This is returned for the following AD syntax 
        type:
        
        Object(DN-String)
        */
        try
        {
            ActiveDs.IADsDNWithString dnWithString;
            dnWithString = (ActiveDs.IADsDNWithString)obj;
            return "IADsDNWithString:" + 
                dnWithString.DNString + ":" + 
                dnWithString.StringValue;
        }
        catch (System.InvalidCastException)
        {
        }

        throw new System.ArgumentException("Unknown COM Object type.");
    }
    else
    {
        throw new System.ArgumentException("Object is not a COM Object.");
    }
}