Partager via


Types de propriétés

Différents types de propriétés sont utilisés avec les objets d'annuaire. Dans le schéma Active Directory, ces types de propriétés sont appelés syntaxes de l'attribut. Pour plus d'informations sur les syntaxes de l'attribut et pour obtenir la liste des syntaxes de l'attribut pouvant être utilisées dans Active Directory, reportez-vous à la rubrique Syntaxes for Active Directory Attributes dans MSDN Library, à l'adresse https://msdn.microsoft.com/libraryadschema.syntaxes.

Les rubriques suivantes contiennent des exemples de code montrant comment lire et écrire les types de propriétés en utilisant System.DirectoryServices :

Types de données interprétés

Il existe deux façons d'extraire les valeurs de propriété depuis l'espace de noms System.DirectoryServices. La première consiste à utiliser les membres de la propriété Properties. La seconde consiste à utiliser les membres de la collection ResultPropertyValueCollection obtenus avec la classe DirectorySearcher. Chaque membre retourne des objets génériques dont le type de données dépend du type de données de schéma de la propriété. La propriété Properties renvoie le même type d'objet que la méthode IADs.GetInfoEx. (Pour plus d'informations sur la méthode IADs.GetInfoEx, reportez-vous à la rubrique IADs::GetInfoEx dans MSDN Library, à l'adresse https://msdn.microsoft.com/library. La propriété Item convertit certains types de données en types de données .NET Framework. Le tableau suivant montre les types de schémas Active Directory et les types de données interprétés et non interprétés correspondants. Pour plus d'informations sur un type de schéma Active Directory ou un nom d'interface COM figurant dans le tableau ci-dessous, reportez-vous à la rubrique correspondant au type ou au nom d'interface COM spécifique dans MSDN Library, à l'adresse https://msdn.microsoft.com/library.

Type de schéma Active Directory Type non interprété (comme retourné par Properties) Type interprété (comme retourné par 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

Objet COM pouvant être casté en IADsLargeInteger.

Int64

LargeInteger

Objet COM pouvant être casté en IADsLargeInteger.

Int64

Object(Access-Point)

Non pris en charge

Non pris en charge

Object(DN-Binary)

Objet COM pouvant être casté en IADsDNWithBinary.

String contenant le nom unique et les données binaires au format spécifié par Object(DN-Binary).

Object(DN-String)

Objet COM pouvant être casté en IADsDNWithString.

String contenant le nom unique et les données de chaîne au format spécifié par Object(DN-String).

Object(DS-DN)

String

String

Object(OR-Name)

Objet COM pouvant être casté en IADsDNWithBinary.

String contenant le nom unique et les données binaires au format spécifié par 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)

Objet COM pouvant être casté en 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

Interprétation des valeurs de propriétés d'un objet ADSI

Pour certains types de syntaxe Active Directory, tels que LargeIntegeradschema.s_largeinteger, System.DirectoryServices renvoie la valeur de propriété comme objet COM. Cet objet COM doit être converti en type ADSI approprié afin d'obtenir le réel type de propriété. Par exemple, la propriété lastLogon appartient à la syntaxe Interval. System.DirectoryServices renvoie la valeur de propriété pour une syntaxe Interval comme objet COM prenant en charge l'interface IADsLargeInteger. Pour plus d'informations sur ces éléments, reportez-vous aux rubriques LargeInteger, Interval, IADsLargeInteger et lastLogon dans MSDN Library, à l'adresse https://msdn.microsoft.com/library.

L'exemple C# ci-dessous indique comment obtenir l'interface IADsLargeInteger à partir de l'objet COM en castant l'objet COM en objet ActiveDs.IADsLargeInteger. Si vous développez une application utilisant des objets dans l'espace de noms ActiveDS, référencez la bibliothèque de types ActiveDS, activeds.lbs, lors de la compilation et de la liaison de l'application.

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

Le tableau suivant répertorie les types de syntaxe que System.DirectoryServices retourne comme objets COM et les interfaces ADSI associées à chaque type de syntaxe. Pour plus d'informations sur un type de syntaxe ou une interface ADSI figurant dans le tableau ci-dessous, reportez-vous à la rubrique correspondant à ce type de syntaxe ou à cette interface ADSI spécifique dans MSDN Library, à l'adresse https://msdn.microsoft.com/library.

Type de syntaxe Interface ADSI

Interval

IADsLargeInteger

LargeInteger

IADsLargeInteger

Object(DN-Binary)

IADsDNWithBinary

Object(DN-String)

IADsDNWithString

Object(OR-Name)

IADsDNWithBinary

String(NT-Sec-Desc)

IADsSecurityDescriptor

L'exemple C# suivant montre comment obtenir l'interface appropriée pour un objet COM ADSI. Cet exemple utilise l'exception InvalidCastException pour déterminer si la conversion est valide.

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.");
    }
}